-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
594 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 0 additions & 58 deletions
58
sdk/core/Azure.Core/src/Pipeline/Internal/BufferResponsePolicy.cs
This file was deleted.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
sdk/core/Azure.Core/src/Pipeline/Internal/ReadTimeoutStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Core.Pipeline | ||
{ | ||
/// <summary> | ||
/// Read-only Stream that will throw a <see cref="OperationCanceledException"/> if it has to wait longer than a configurable timeout to read more data | ||
/// </summary> | ||
internal class ReadTimeoutStream : ReadOnlyStream | ||
{ | ||
private readonly Stream _stream; | ||
private TimeSpan _readTimeout; | ||
private CancellationTokenSource _cancellationTokenSource; | ||
|
||
public ReadTimeoutStream(Stream stream, TimeSpan readTimeout) | ||
{ | ||
_stream = stream; | ||
_readTimeout = readTimeout; | ||
UpdateReadTimeout(); | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
} | ||
|
||
public override int Read(byte[] buffer, int offset, int count) | ||
{ | ||
return _stream.Read(buffer, offset, count); | ||
} | ||
|
||
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken) | ||
{ | ||
var source = StartTimeout(cancellationToken, out bool dispose); | ||
try | ||
{ | ||
return await _stream.ReadAsync(buffer, offset, count, source.Token).ConfigureAwait(false); | ||
} | ||
finally | ||
{ | ||
StopTimeout(source, dispose); | ||
} | ||
} | ||
|
||
private CancellationTokenSource StartTimeout(CancellationToken additionalToken, out bool dispose) | ||
{ | ||
if (_cancellationTokenSource.IsCancellationRequested) | ||
{ | ||
_cancellationTokenSource = new CancellationTokenSource(); | ||
} | ||
|
||
CancellationTokenSource source; | ||
if (additionalToken.CanBeCanceled) | ||
{ | ||
source = CancellationTokenSource.CreateLinkedTokenSource(additionalToken, _cancellationTokenSource.Token); | ||
dispose = true; | ||
} | ||
else | ||
{ | ||
source = _cancellationTokenSource; | ||
dispose = false; | ||
} | ||
|
||
_cancellationTokenSource.CancelAfter(_readTimeout); | ||
|
||
return source; | ||
} | ||
|
||
private void StopTimeout(CancellationTokenSource source, bool dispose) | ||
{ | ||
_cancellationTokenSource.CancelAfter(Timeout.InfiniteTimeSpan); | ||
if (dispose) | ||
{ | ||
source.Dispose(); | ||
} | ||
} | ||
|
||
public override long Seek(long offset, SeekOrigin origin) | ||
{ | ||
return _stream.Seek(offset, origin); | ||
} | ||
|
||
public override bool CanRead => _stream.CanRead; | ||
public override bool CanSeek => _stream.CanSeek; | ||
public override long Length => _stream.Length; | ||
|
||
public override long Position | ||
{ | ||
get => _stream.Position; | ||
set => _stream.Position = value; | ||
} | ||
|
||
public override int ReadTimeout | ||
{ | ||
get => (int) _readTimeout.TotalMilliseconds; | ||
set | ||
{ | ||
_readTimeout = TimeSpan.FromMilliseconds(value); | ||
UpdateReadTimeout(); | ||
} | ||
} | ||
|
||
private void UpdateReadTimeout() | ||
{ | ||
try | ||
{ | ||
_stream.ReadTimeout = (int) _readTimeout.TotalMilliseconds; | ||
} | ||
catch | ||
{ | ||
// ignore | ||
} | ||
} | ||
|
||
public override void Close() | ||
{ | ||
_stream.Close(); | ||
} | ||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
base.Dispose(disposing); | ||
_stream.Dispose(); | ||
_cancellationTokenSource.Dispose(); | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
sdk/core/Azure.Core/src/Pipeline/Internal/ResponseBodyPolicy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Buffers; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.Buffers; | ||
|
||
namespace Azure.Core.Pipeline | ||
{ | ||
/// <summary> | ||
/// Pipeline policy to buffer response content or add a timeout to response content managed by the client | ||
/// </summary> | ||
internal class ResponseBodyPolicy : HttpPipelinePolicy | ||
{ | ||
// Same value as Stream.CopyTo uses by default | ||
private const int DefaultCopyBufferSize = 81920; | ||
|
||
private readonly TimeSpan _networkTimeout; | ||
|
||
public ResponseBodyPolicy(TimeSpan networkTimeout) | ||
{ | ||
_networkTimeout = networkTimeout; | ||
} | ||
|
||
public override async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
{ | ||
await ProcessAsync(message, pipeline, true).ConfigureAwait(false); | ||
} | ||
|
||
public override void Process(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline) | ||
{ | ||
ProcessAsync(message, pipeline, false).EnsureCompleted(); | ||
} | ||
|
||
private async ValueTask ProcessAsync(HttpMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async) | ||
{ | ||
CancellationToken oldToken = message.CancellationToken; | ||
using CancellationTokenSource cts = CancellationTokenSource.CreateLinkedTokenSource(oldToken); | ||
|
||
cts.CancelAfter(_networkTimeout); | ||
|
||
try | ||
{ | ||
message.CancellationToken = cts.Token; | ||
if (async) | ||
{ | ||
await ProcessNextAsync(message, pipeline).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
ProcessNext(message, pipeline); | ||
} | ||
} | ||
finally | ||
{ | ||
message.CancellationToken = oldToken; | ||
cts.CancelAfter(Timeout.Infinite); | ||
} | ||
|
||
Stream? responseContentStream = message.Response.ContentStream; | ||
if (responseContentStream == null || responseContentStream.CanSeek) | ||
{ | ||
return; | ||
} | ||
|
||
if (message.BufferResponse) | ||
{ | ||
var bufferedStream = new MemoryStream(); | ||
if (async) | ||
{ | ||
await CopyToAsync(responseContentStream, bufferedStream, cts).ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
CopyTo(responseContentStream, bufferedStream, message.CancellationToken); | ||
} | ||
|
||
responseContentStream.Dispose(); | ||
bufferedStream.Position = 0; | ||
message.Response.ContentStream = bufferedStream; | ||
} | ||
else if (_networkTimeout != Timeout.InfiniteTimeSpan) | ||
{ | ||
message.Response.ContentStream = new ReadTimeoutStream(responseContentStream, _networkTimeout); | ||
} | ||
} | ||
|
||
private async Task CopyToAsync(Stream source, Stream destination, CancellationTokenSource cancellationTokenSource) | ||
{ | ||
byte[] buffer = ArrayPool<byte>.Shared.Rent(DefaultCopyBufferSize); | ||
try | ||
{ | ||
while (true) | ||
{ | ||
cancellationTokenSource.CancelAfter(_networkTimeout); | ||
int bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationTokenSource.Token).ConfigureAwait(false); | ||
if (bytesRead == 0) break; | ||
await destination.WriteAsync(new ReadOnlyMemory<byte>(buffer, 0, bytesRead), cancellationTokenSource.Token).ConfigureAwait(false); | ||
} | ||
} | ||
finally | ||
{ | ||
cancellationTokenSource.CancelAfter(Timeout.InfiniteTimeSpan); | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
} | ||
|
||
private static void CopyTo(Stream source, Stream destination, CancellationToken cancellationToken) | ||
{ | ||
byte[] buffer = ArrayPool<byte>.Shared.Rent(DefaultCopyBufferSize); | ||
try | ||
{ | ||
int read; | ||
while ((read = source.Read(buffer, 0, buffer.Length)) != 0) | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
destination.Write(buffer, 0, read); | ||
} | ||
} | ||
finally | ||
{ | ||
ArrayPool<byte>.Shared.Return(buffer); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.