|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Threading; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using System.Threading.Tasks.Sources; |
| 6 | + |
| 7 | +namespace StackExchange.Redis; |
| 8 | + |
| 9 | +internal sealed class BufferedAsyncStreamWriter : CycleBufferStreamWriter, IValueTaskSource |
| 10 | +{ |
| 11 | + private ManualResetValueTaskSourceCore<bool> _readerTask; |
| 12 | + |
| 13 | + public BufferedAsyncStreamWriter(Stream target, CancellationToken cancellationToken = default) |
| 14 | + : base(target, cancellationToken) |
| 15 | + { |
| 16 | + WriteComplete = Task.Run(CopyOutAsync, cancellationToken); |
| 17 | + _readerTask.RunContinuationsAsynchronously = true; // we never want the flusher to take over the copying |
| 18 | + } |
| 19 | + |
| 20 | + public override Task WriteComplete { get; } |
| 21 | + |
| 22 | + private async Task CopyOutAsync() |
| 23 | + { |
| 24 | + try |
| 25 | + { |
| 26 | + while (true) |
| 27 | + { |
| 28 | + ValueTask pending = AwaitWake(); |
| 29 | + if (!pending.IsCompleted) |
| 30 | + { |
| 31 | + lock (this) |
| 32 | + { |
| 33 | + // double-checked marking inactive |
| 34 | + if (!pending.IsCompleted) OnWriterInactive(); // update state flags |
| 35 | + } |
| 36 | + } |
| 37 | + // await activation and check status; |
| 38 | + await pending.ConfigureAwait(false); |
| 39 | + |
| 40 | + StateFlags stateFlags; |
| 41 | + while (true) |
| 42 | + { |
| 43 | + ReadOnlyMemory<byte> memory; |
| 44 | + lock (this) |
| 45 | + { |
| 46 | + stateFlags = State; |
| 47 | + var minBytes = (stateFlags & StateFlags.Flush) == 0 ? -1 : 1; |
| 48 | + if (!GetFirstChunkInsideLock(minBytes, out memory)) |
| 49 | + { |
| 50 | + // out of data; remove flush flag and wait for more work |
| 51 | + stateFlags &= ~StateFlags.Flush; |
| 52 | + break; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if (IsFaulted) ThrowCompleteOrFaulted(); // this is cheap to check ongoing |
| 57 | + if (!memory.IsEmpty) |
| 58 | + { |
| 59 | + OnWritten(memory.Length); |
| 60 | + OnDebugBufferLog(memory); |
| 61 | + |
| 62 | + await Target.WriteAsync(memory, CancellationToken).ConfigureAwait(false); |
| 63 | + } |
| 64 | + |
| 65 | + lock (this) |
| 66 | + { |
| 67 | + DiscardCommitted(memory.Length); |
| 68 | + } |
| 69 | + } |
| 70 | + await Target.FlushAsync(CancellationToken).ConfigureAwait(false); |
| 71 | + |
| 72 | + if ((stateFlags & StateFlags.Closed) != 0) break; |
| 73 | + } |
| 74 | + |
| 75 | + // recycle on clean exit (only), since we know the buffers aren't being used |
| 76 | + lock (this) |
| 77 | + { |
| 78 | + ReleaseBuffer(); |
| 79 | + } |
| 80 | + } |
| 81 | + catch (Exception ex) |
| 82 | + { |
| 83 | + Complete(ex); |
| 84 | + } |
| 85 | + // note we do *not* close the stream here - we have to settle for flushing; Close is explicit |
| 86 | + } |
| 87 | + |
| 88 | + private ValueTask AwaitWake() |
| 89 | + { |
| 90 | + lock (this) // guard all transitions |
| 91 | + { |
| 92 | + return new(this, _readerTask.Version); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + void IValueTaskSource.GetResult(short token) |
| 97 | + { |
| 98 | + lock (this) // guard all transitions |
| 99 | + { |
| 100 | + _readerTask.GetResult(token); // may throw, note |
| 101 | + _readerTask.Reset(); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + ValueTaskSourceStatus IValueTaskSource.GetStatus(short token) |
| 106 | + { |
| 107 | + lock (this) // guard all transitions |
| 108 | + { |
| 109 | + return _readerTask.GetStatus(token); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + void IValueTaskSource.OnCompleted( |
| 114 | + Action<object?> continuation, |
| 115 | + object? state, |
| 116 | + short token, |
| 117 | + ValueTaskSourceOnCompletedFlags flags) |
| 118 | + { |
| 119 | + lock (this) // guard all transitions |
| 120 | + { |
| 121 | + _readerTask.OnCompleted(continuation, state, token, flags); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + protected override void OnWakeReader() |
| 126 | + { |
| 127 | + lock (this) // guard all transitions |
| 128 | + { |
| 129 | + _readerTask.SetResult(true); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments