| | | 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 | | { |
| | 60 | 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 | | |
| | 60 | 67 | | public RetriableStreamImpl(Stream initialStream, Func<long, Stream> streamFactory, Func<long, ValueTask<Stre |
| | | 68 | | { |
| | | 69 | | try |
| | | 70 | | { |
| | 60 | 71 | | _length = EnsureStream(initialStream).Length; |
| | 60 | 72 | | } |
| | 0 | 73 | | catch (Exception ex) |
| | | 74 | | { |
| | 0 | 75 | | _lengthException = ExceptionDispatchInfo.Capture(ex); |
| | 0 | 76 | | } |
| | | 77 | | |
| | 60 | 78 | | _currentStream = EnsureStream(initialStream); |
| | 60 | 79 | | _streamFactory = streamFactory; |
| | 60 | 80 | | _responseClassifier = responseClassifier; |
| | 60 | 81 | | _asyncStreamFactory = asyncStreamFactory; |
| | 60 | 82 | | _maxRetries = maxRetries; |
| | 60 | 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 | | { |
| | 120 | 96 | | var result = await _currentStream.ReadAsync(buffer, offset, count, cancellationToken).ConfigureA |
| | 118 | 97 | | _position += result; |
| | 118 | 98 | | return result; |
| | | 99 | | } |
| | | 100 | | catch (Exception e) |
| | | 101 | | { |
| | 2 | 102 | | await RetryAsync(e, true, cancellationToken).ConfigureAwait(false); |
| | 2 | 103 | | } |
| | | 104 | | } |
| | 118 | 105 | | } |
| | | 106 | | |
| | | 107 | | private async Task RetryAsync(Exception exception, bool async, CancellationToken cancellationToken) |
| | | 108 | | { |
| | 2 | 109 | | bool isNonCustomerCancelledException = exception is OperationCanceledException && |
| | 2 | 110 | | !cancellationToken.IsCancellationRequested; |
| | | 111 | | |
| | 2 | 112 | | if (!_responseClassifier.IsRetriableException(exception) && !isNonCustomerCancelledException) |
| | | 113 | | { |
| | 0 | 114 | | ExceptionDispatchInfo.Capture(exception).Throw(); |
| | | 115 | | } |
| | | 116 | | |
| | 2 | 117 | | if (_exceptions == null) |
| | | 118 | | { |
| | 2 | 119 | | _exceptions = new List<Exception>(); |
| | | 120 | | } |
| | | 121 | | |
| | 2 | 122 | | _exceptions.Add(exception); |
| | | 123 | | |
| | 2 | 124 | | _retryCount++; |
| | | 125 | | |
| | 2 | 126 | | if (_retryCount > _maxRetries) |
| | | 127 | | { |
| | 0 | 128 | | throw new AggregateException($"Retry failed after {_retryCount} tries", _exceptions); |
| | | 129 | | } |
| | | 130 | | |
| | 2 | 131 | | _currentStream.Dispose(); |
| | | 132 | | |
| | 2 | 133 | | _currentStream = EnsureStream(async ? (await _asyncStreamFactory(_position).ConfigureAwait(false)) : _st |
| | 2 | 134 | | } |
| | | 135 | | |
| | | 136 | | public override int Read(byte[] buffer, int offset, int count) |
| | | 137 | | { |
| | | 138 | | while (true) |
| | | 139 | | { |
| | | 140 | | try |
| | | 141 | | { |
| | 0 | 142 | | var result = _currentStream.Read(buffer, offset, count); |
| | 0 | 143 | | _position += result; |
| | 0 | 144 | | return result; |
| | | 145 | | |
| | | 146 | | } |
| | 0 | 147 | | catch (Exception e) |
| | | 148 | | { |
| | 0 | 149 | | RetryAsync(e, false, default).EnsureCompleted(); |
| | 0 | 150 | | } |
| | | 151 | | } |
| | 0 | 152 | | } |
| | | 153 | | |
| | 28 | 154 | | public override bool CanRead => _currentStream.CanRead; |
| | 26 | 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 | | { |
| | 122 | 173 | | if (stream == null) |
| | | 174 | | { |
| | 0 | 175 | | throw new InvalidOperationException("The response didn't have content"); |
| | | 176 | | } |
| | | 177 | | |
| | 122 | 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 | | { |
| | 28 | 200 | | base.Dispose(disposing); |
| | 28 | 201 | | _currentStream?.Dispose(); |
| | 28 | 202 | | } |
| | | 203 | | } |
| | | 204 | | } |
| | | 205 | | } |