-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add response read timeouts #10128
Merged
Merged
Add response read timeouts #10128
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c8e839e
Add operation timeout
pakrym f603366
Merge branch 'master' into pakrym/add-operation-timeout
pakrym 166ad3a
fixes
pakrym 48ee642
Fix the test.
pakrym 5550646
OrEqual.
pakrym 1cd86e5
Use ReadOnlyStream
pakrym b5fa279
Changelog
pakrym 4208483
FB
pakrym c19e726
undo tracing
pakrym d38feb2
dispose and close
pakrym 09c3ff8
Add a test for ReadTimeoutProperty.
pakrym 8aea8a9
Make timeout behavior consistent.
pakrym 74e8556
Stream comment.
pakrym 3588262
API listing and CHANGELOG.md
pakrym 22737a6
Words.
pakrym 81453e7
BufferSize comment.
pakrym File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
heaths marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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) | ||
AlexanderSher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 | ||
pakrym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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(); | ||
pakrym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
_cancellationTokenSource.Dispose(); | ||
} | ||
} | ||
} |
128 changes: 128 additions & 0 deletions
128
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,128 @@ | ||
// 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 | ||
pakrym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private const int DefaultCopyBufferSize = 81920; | ||
pakrym marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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) | ||
AlexanderSher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we check if the timeout was changed, and if not, use the shared policy