| | | 1 | | // Copyright (c) ZeroC, Inc. |
| | | 2 | | |
| | | 3 | | using System.Buffers; |
| | | 4 | | using System.Diagnostics; |
| | | 5 | | |
| | | 6 | | namespace IceRpc.Transports.Slic.Internal; |
| | | 7 | | |
| | | 8 | | /// <summary>Decorates <see cref="ReadAsync" /> to fail if no byte is received for over idle timeout. Also optionally |
| | | 9 | | /// decorates both <see cref="ReadAsync"/> and <see cref="WriteAsync" /> to schedule pings that prevent both the local |
| | | 10 | | /// and remote idle timers from expiring.</summary> |
| | | 11 | | internal class SlicDuplexConnectionDecorator : IDuplexConnection |
| | | 12 | | { |
| | | 13 | | private readonly IDuplexConnection _decoratee; |
| | 1376 | 14 | | private TimeSpan _idleTimeout = Timeout.InfiniteTimeSpan; |
| | 1376 | 15 | | private readonly CancellationTokenSource _readCts = new(); |
| | | 16 | | |
| | | 17 | | private readonly Timer? _readTimer; |
| | | 18 | | private readonly Timer? _writeTimer; |
| | | 19 | | |
| | | 20 | | public Task<TransportConnectionInformation> ConnectAsync(CancellationToken cancellationToken) => |
| | 1332 | 21 | | _decoratee.ConnectAsync(cancellationToken); |
| | | 22 | | |
| | | 23 | | public void Dispose() |
| | 1374 | 24 | | { |
| | 1374 | 25 | | _decoratee.Dispose(); |
| | 1374 | 26 | | _readCts.Dispose(); |
| | | 27 | | |
| | | 28 | | // Using Dispose is fine, there's no need to wait for the keep alive action to terminate if it's running. |
| | 1374 | 29 | | _readTimer?.Dispose(); |
| | 1374 | 30 | | _writeTimer?.Dispose(); |
| | 1374 | 31 | | } |
| | | 32 | | |
| | | 33 | | public ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken cancellationToken) |
| | 79271 | 34 | | { |
| | 79271 | 35 | | return _idleTimeout == Timeout.InfiniteTimeSpan ? |
| | 79271 | 36 | | _decoratee.ReadAsync(buffer, cancellationToken) : |
| | 79271 | 37 | | PerformReadAsync(); |
| | | 38 | | |
| | | 39 | | async ValueTask<int> PerformReadAsync() |
| | 77974 | 40 | | { |
| | | 41 | | try |
| | 77974 | 42 | | { |
| | 77974 | 43 | | using CancellationTokenRegistration _ = cancellationToken.UnsafeRegister( |
| | 548 | 44 | | cts => ((CancellationTokenSource)cts!).Cancel(), |
| | 77974 | 45 | | _readCts); |
| | 77974 | 46 | | _readCts.CancelAfter(_idleTimeout); // enable idle timeout before reading |
| | | 47 | | |
| | 77974 | 48 | | int bytesRead = await _decoratee.ReadAsync(buffer, _readCts.Token).ConfigureAwait(false); |
| | | 49 | | |
| | | 50 | | // After each successful read, we schedule one ping some time in the future. |
| | 77061 | 51 | | if (bytesRead > 0) |
| | 76802 | 52 | | { |
| | 76802 | 53 | | ResetReadTimer(); |
| | 76802 | 54 | | } |
| | | 55 | | // When 0, the other side called ShutdownWriteAsync, so there is no point to send a ping since we can't |
| | | 56 | | // get back a pong. |
| | | 57 | | |
| | 77061 | 58 | | return bytesRead; |
| | | 59 | | } |
| | 461 | 60 | | catch (OperationCanceledException) |
| | 461 | 61 | | { |
| | 461 | 62 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 63 | | |
| | 2 | 64 | | throw new IceRpcException( |
| | 2 | 65 | | IceRpcError.ConnectionIdle, |
| | 2 | 66 | | $"The connection did not receive any bytes for over {_idleTimeout.TotalSeconds} s."); |
| | | 67 | | } |
| | | 68 | | finally |
| | 77974 | 69 | | { |
| | 77974 | 70 | | _readCts.CancelAfter(Timeout.InfiniteTimeSpan); // disable idle timeout if not canceled |
| | 77974 | 71 | | } |
| | 77061 | 72 | | } |
| | 79271 | 73 | | } |
| | | 74 | | |
| | | 75 | | public Task ShutdownWriteAsync(CancellationToken cancellationToken) => |
| | 272 | 76 | | _decoratee.ShutdownWriteAsync(cancellationToken); |
| | | 77 | | |
| | | 78 | | public ValueTask WriteAsync(ReadOnlySequence<byte> buffer, CancellationToken cancellationToken) |
| | 8903 | 79 | | { |
| | 8903 | 80 | | return _idleTimeout == Timeout.InfiniteTimeSpan ? |
| | 8903 | 81 | | _decoratee.WriteAsync(buffer, cancellationToken) : |
| | 8903 | 82 | | PerformWriteAsync(); |
| | | 83 | | |
| | | 84 | | async ValueTask PerformWriteAsync() |
| | 8254 | 85 | | { |
| | 8254 | 86 | | await _decoratee.WriteAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | | 87 | | |
| | | 88 | | // After each successful write, we schedule one ping some time in the future. Since each ping is itself a |
| | | 89 | | // write, if there is no application activity at all, we'll send successive pings at regular intervals. |
| | 8240 | 90 | | ResetWriteTimer(); |
| | 8240 | 91 | | } |
| | 8903 | 92 | | } |
| | | 93 | | |
| | | 94 | | /// <summary>Constructs a decorator that does nothing until it is enabled by a call to <see cref="Enable"/>. |
| | | 95 | | /// </summary> |
| | 2752 | 96 | | internal SlicDuplexConnectionDecorator(IDuplexConnection decoratee) => _decoratee = decoratee; |
| | | 97 | | |
| | | 98 | | /// <summary>Constructs a decorator that does nothing until it is enabled by a call to <see cref="Enable"/>. |
| | | 99 | | /// </summary> |
| | | 100 | | internal SlicDuplexConnectionDecorator(IDuplexConnection decoratee, Action sendReadPing, Action sendWritePing) |
| | 697 | 101 | | : this(decoratee) |
| | 697 | 102 | | { |
| | 726 | 103 | | _readTimer = new Timer(_ => sendReadPing()); |
| | 699 | 104 | | _writeTimer = new Timer(_ => sendWritePing()); |
| | 697 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary>Sets the idle timeout and schedules pings once the connection is established.</summary>. |
| | | 108 | | internal void Enable(TimeSpan idleTimeout) |
| | 1222 | 109 | | { |
| | 1222 | 110 | | Debug.Assert(idleTimeout != Timeout.InfiniteTimeSpan); |
| | 1222 | 111 | | _idleTimeout = idleTimeout; |
| | | 112 | | |
| | 1222 | 113 | | ResetReadTimer(); |
| | 1222 | 114 | | ResetWriteTimer(); |
| | 1222 | 115 | | } |
| | | 116 | | |
| | | 117 | | /// <summary>Resets the read timer. We send a "read" ping when this timer expires.</summary> |
| | | 118 | | /// <remarks>This method is no-op unless this decorator is constructed with send ping actions.</remarks> |
| | 78024 | 119 | | private void ResetReadTimer() => _readTimer?.Change(_idleTimeout * 0.5, Timeout.InfiniteTimeSpan); |
| | | 120 | | |
| | | 121 | | /// <summary>Resets the write timer. We send a "write" ping when this timer expires.</summary> |
| | | 122 | | /// <remarks>This method is no-op unless this decorator is constructed with send ping actions.</remarks> |
| | | 123 | | // The write timer factor (0.6) was chosen to be greater than the read timer factor (0.5). This way, when the |
| | | 124 | | // connection is completely idle, the read timer expires before the write timer and has time to send a ping that |
| | | 125 | | // resets the write timer. This reduces the likelihood of duplicate "keep alive" pings. |
| | 9462 | 126 | | private void ResetWriteTimer() => _writeTimer?.Change(_idleTimeout * 0.6, Timeout.InfiniteTimeSpan); |
| | | 127 | | } |