| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable disable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Diagnostics; |
| | 9 | | using System.IO; |
| | 10 | | using System.Runtime.ExceptionServices; |
| | 11 | | using System.Threading; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | |
|
| | 14 | | namespace Azure.Core.Pipeline |
| | 15 | | { |
| | 16 | | internal static class RetriableStream |
| | 17 | | { |
| | 18 | | public static Stream Create( |
| | 19 | | Func<long, Stream> responseFactory, |
| | 20 | | Func<long, ValueTask<Stream>> asyncResponseFactory, |
| | 21 | | ResponseClassifier responseClassifier, |
| | 22 | | int maxRetries) |
| | 23 | | { |
| 0 | 24 | | return Create(responseFactory(0), responseFactory, asyncResponseFactory, responseClassifier, maxRetries); |
| | 25 | | } |
| | 26 | |
|
| | 27 | | public static async Task<Stream> CreateAsync( |
| | 28 | | Func<long, Stream> responseFactory, |
| | 29 | | Func<long, ValueTask<Stream>> asyncResponseFactory, |
| | 30 | | ResponseClassifier responseClassifier, |
| | 31 | | int maxRetries) |
| | 32 | | { |
| 0 | 33 | | return Create(await asyncResponseFactory(0).ConfigureAwait(false), responseFactory, asyncResponseFactory, re |
| 0 | 34 | | } |
| | 35 | |
|
| | 36 | | public static Stream Create( |
| | 37 | | Stream initialResponse, |
| | 38 | | Func<long, Stream> streamFactory, |
| | 39 | | Func<long, ValueTask<Stream>> asyncResponseFactory, |
| | 40 | | ResponseClassifier responseClassifier, |
| | 41 | | int maxRetries) |
| | 42 | | { |
| 1432 | 43 | | return new RetriableStreamImpl(initialResponse, streamFactory, asyncResponseFactory, responseClassifier, max |
| | 44 | | } |
| | 45 | |
|
| | 46 | | private class RetriableStreamImpl : Stream |
| | 47 | | { |
| | 48 | | private readonly ResponseClassifier _responseClassifier; |
| | 49 | |
|
| | 50 | | private readonly Func<long, Stream> _streamFactory; |
| | 51 | |
|
| | 52 | | private readonly Func<long, ValueTask<Stream>> _asyncStreamFactory; |
| | 53 | |
|
| | 54 | | private readonly int _maxRetries; |
| | 55 | |
|
| | 56 | | private readonly long _length; |
| | 57 | | private readonly ExceptionDispatchInfo _lengthException; |
| | 58 | |
|
| | 59 | | private Stream _currentStream; |
| | 60 | |
|
| | 61 | | private long _position; |
| | 62 | |
|
| | 63 | | private int _retryCount; |
| | 64 | |
|
| | 65 | | private List<Exception> _exceptions; |
| | 66 | |
|
| 1432 | 67 | | public RetriableStreamImpl(Stream initialStream, Func<long, Stream> streamFactory, Func<long, ValueTask<Stre |
| | 68 | | { |
| | 69 | | try |
| | 70 | | { |
| 1432 | 71 | | _length = EnsureStream(initialStream).Length; |
| 1432 | 72 | | } |
| 0 | 73 | | catch (Exception ex) |
| | 74 | | { |
| 0 | 75 | | _lengthException = ExceptionDispatchInfo.Capture(ex); |
| 0 | 76 | | } |
| | 77 | |
|
| 1432 | 78 | | _currentStream = EnsureStream(initialStream); |
| 1432 | 79 | | _streamFactory = streamFactory; |
| 1432 | 80 | | _responseClassifier = responseClassifier; |
| 1432 | 81 | | _asyncStreamFactory = asyncStreamFactory; |
| 1432 | 82 | | _maxRetries = maxRetries; |
| 1432 | 83 | | } |
| | 84 | |
|
| | 85 | | public override long Seek(long offset, SeekOrigin origin) |
| | 86 | | { |
| 0 | 87 | | throw new NotSupportedException(); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellati |
| | 91 | | { |
| | 92 | | while (true) |
| | 93 | | { |
| | 94 | | try |
| | 95 | | { |
| 1551806 | 96 | | var result = await _currentStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureA |
| 1551806 | 97 | | _position += result; |
| 1551806 | 98 | | return result; |
| | 99 | | } |
| | 100 | | catch (Exception e) |
| | 101 | | { |
| 0 | 102 | | await RetryAsync(e, true, cancellationToken).ConfigureAwait(false); |
| 0 | 103 | | } |
| | 104 | | } |
| 1551806 | 105 | | } |
| | 106 | |
|
| | 107 | | private async Task RetryAsync(Exception exception, bool async, CancellationToken cancellationToken) |
| | 108 | | { |
| 0 | 109 | | bool isNonCustomerCancelledException = exception is OperationCanceledException && |
| 0 | 110 | | !cancellationToken.IsCancellationRequested; |
| | 111 | |
|
| 0 | 112 | | if (!_responseClassifier.IsRetriableException(exception) && !isNonCustomerCancelledException) |
| | 113 | | { |
| 0 | 114 | | ExceptionDispatchInfo.Capture(exception).Throw(); |
| | 115 | | } |
| | 116 | |
|
| 0 | 117 | | if (_exceptions == null) |
| | 118 | | { |
| 0 | 119 | | _exceptions = new List<Exception>(); |
| | 120 | | } |
| | 121 | |
|
| 0 | 122 | | _exceptions.Add(exception); |
| | 123 | |
|
| 0 | 124 | | _retryCount++; |
| | 125 | |
|
| 0 | 126 | | if (_retryCount > _maxRetries) |
| | 127 | | { |
| 0 | 128 | | throw new AggregateException($"Retry failed after {_retryCount} tries", _exceptions); |
| | 129 | | } |
| | 130 | |
|
| 0 | 131 | | _currentStream.Dispose(); |
| | 132 | |
|
| 0 | 133 | | _currentStream = EnsureStream(async ? (await _asyncStreamFactory(_position).ConfigureAwait(false)) : _st |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | public override int Read(byte[] buffer, int offset, int count) |
| | 137 | | { |
| | 138 | | while (true) |
| | 139 | | { |
| | 140 | | try |
| | 141 | | { |
| 42 | 142 | | var result = _currentStream.Read(buffer, offset, count); |
| 42 | 143 | | _position += result; |
| 42 | 144 | | return result; |
| | 145 | |
|
| | 146 | | } |
| 0 | 147 | | catch (Exception e) |
| | 148 | | { |
| 0 | 149 | | RetryAsync(e, false, default).EnsureCompleted(); |
| 0 | 150 | | } |
| | 151 | | } |
| 42 | 152 | | } |
| | 153 | |
|
| 356 | 154 | | public override bool CanRead => _currentStream.CanRead; |
| 432 | 155 | | public override bool CanSeek { get; } = false; |
| | 156 | | public override long Length |
| | 157 | | { |
| | 158 | | get |
| | 159 | | { |
| 0 | 160 | | _lengthException?.Throw(); |
| 0 | 161 | | return _length; |
| | 162 | | } |
| | 163 | | } |
| | 164 | |
|
| | 165 | | public override long Position |
| | 166 | | { |
| 0 | 167 | | get => _position; |
| 0 | 168 | | set => throw new NotSupportedException(); |
| | 169 | | } |
| | 170 | |
|
| | 171 | | private static Stream EnsureStream(Stream stream) |
| | 172 | | { |
| 2864 | 173 | | if (stream == null) |
| | 174 | | { |
| 0 | 175 | | throw new InvalidOperationException("The response didn't have content"); |
| | 176 | | } |
| | 177 | |
|
| 2864 | 178 | | return stream; |
| | 179 | | } |
| | 180 | |
|
| 0 | 181 | | public override bool CanWrite => false; |
| | 182 | |
|
| | 183 | | public override void Write(byte[] buffer, int offset, int count) |
| | 184 | | { |
| 0 | 185 | | throw new NotSupportedException(); |
| | 186 | | } |
| | 187 | |
|
| | 188 | | public override void SetLength(long value) |
| | 189 | | { |
| 0 | 190 | | throw new NotSupportedException(); |
| | 191 | | } |
| | 192 | |
|
| | 193 | | public override void Flush() |
| | 194 | | { |
| | 195 | | // Flush is allowed on read-only stream |
| 0 | 196 | | } |
| | 197 | |
|
| | 198 | | protected override void Dispose(bool disposing) |
| | 199 | | { |
| 648 | 200 | | base.Dispose(disposing); |
| 648 | 201 | | _currentStream?.Dispose(); |
| 648 | 202 | | } |
| | 203 | | } |
| | 204 | | } |
| | 205 | | } |