OperationLogPipeline.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Channels;
  5. using System.Threading.Tasks;
  6. using Newtonsoft.Json;
  7. namespace Aivfo.OperationLog
  8. {
  9. /// <summary>
  10. /// 异步管道(14§9/§11/§12):
  11. /// 业务线程非阻塞入队(有界 Channel,满了丢弃 + 本地兜底)→ 后台单线程批量取出 → IOplogTransport 发送。
  12. /// 入队绝不阻塞、绝不抛异常影响业务。
  13. /// </summary>
  14. internal sealed class OperationLogPipeline : IDisposable
  15. {
  16. private readonly OperationLogOptions _options;
  17. private readonly IOplogTransport _transport;
  18. private readonly LocalFileWriter _local;
  19. private readonly Channel<OperationLogMessage> _channel;
  20. private readonly CancellationTokenSource _cts = new CancellationTokenSource();
  21. private readonly Task _worker;
  22. private volatile bool _disposed;
  23. public OperationLogPipeline(OperationLogOptions options, IOplogTransport transport, LocalFileWriter local)
  24. {
  25. _options = options;
  26. _transport = transport;
  27. _local = local;
  28. _channel = Channel.CreateBounded<OperationLogMessage>(new BoundedChannelOptions(Math.Max(16, options.QueueCapacity))
  29. {
  30. FullMode = BoundedChannelFullMode.DropWrite, // 满了让 TryWrite 返回 false,由我们走兜底,绝不阻塞
  31. SingleReader = true,
  32. SingleWriter = false
  33. });
  34. _worker = Task.Factory.StartNew(RunLoop, TaskCreationOptions.LongRunning).Unwrap();
  35. }
  36. /// <summary>非阻塞入队。满了走本地兜底(不丢失),永不阻塞/抛异常。</summary>
  37. public void Enqueue(OperationLogMessage msg)
  38. {
  39. if (_disposed || msg == null) return;
  40. try
  41. {
  42. if (!_channel.Writer.TryWrite(msg))
  43. {
  44. // 队列满降级:操作级落本地兜底(不丢),调试级本不该到这(Debug 不入 Kafka 队列)。
  45. _local.WriteFallback(SafeJson(msg));
  46. }
  47. }
  48. catch (Exception ex)
  49. {
  50. _local.WriteSelfError("Enqueue failed: " + ex.Message);
  51. }
  52. }
  53. private async Task RunLoop()
  54. {
  55. var reader = _channel.Reader;
  56. var batch = new List<OperationLogMessage>(_options.BatchSize);
  57. try
  58. {
  59. while (await reader.WaitToReadAsync(_cts.Token).ConfigureAwait(false))
  60. {
  61. batch.Clear();
  62. while (batch.Count < _options.BatchSize && reader.TryRead(out var m))
  63. batch.Add(m);
  64. foreach (var m in batch)
  65. {
  66. var json = SafeJson(m);
  67. try
  68. {
  69. _transport.Send(json);
  70. }
  71. catch (Exception ex)
  72. {
  73. // 发送失败:本地兜底(恢复后可选补送),并记自身错误。
  74. _local.WriteFallback(json);
  75. _local.WriteSelfError("Transport.Send failed: " + ex.Message);
  76. }
  77. }
  78. }
  79. }
  80. catch (OperationCanceledException) { /* 正常退出 */ }
  81. catch (Exception ex)
  82. {
  83. _local.WriteSelfError("Pipeline loop crashed: " + ex.Message);
  84. }
  85. }
  86. private string SafeJson(OperationLogMessage m)
  87. {
  88. try { return JsonConvert.SerializeObject(m); }
  89. catch (Exception ex)
  90. {
  91. _local.WriteSelfError("Message serialize failed: " + ex.Message);
  92. return "{}";
  93. }
  94. }
  95. /// <summary>排空队列并 flush 传输(用于测试/退出)。</summary>
  96. public void Flush(TimeSpan timeout)
  97. {
  98. try
  99. {
  100. var deadline = DateTime.UtcNow + timeout;
  101. // 等待 channel 排空(后台线程在消费)。
  102. while (_channel.Reader.Count > 0 && DateTime.UtcNow < deadline)
  103. Thread.Sleep(20);
  104. _transport.Flush(timeout);
  105. }
  106. catch (Exception ex)
  107. {
  108. _local.WriteSelfError("Flush failed: " + ex.Message);
  109. }
  110. }
  111. public void Dispose()
  112. {
  113. if (_disposed) return;
  114. _disposed = true;
  115. try
  116. {
  117. _channel.Writer.TryComplete();
  118. Flush(TimeSpan.FromSeconds(3));
  119. _cts.Cancel();
  120. _worker.Wait(TimeSpan.FromSeconds(3));
  121. }
  122. catch { /* ignore */ }
  123. finally
  124. {
  125. _transport.Dispose();
  126. _cts.Dispose();
  127. }
  128. }
  129. }
  130. }