Skip to content
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 sync support to retriable stream #6420

Merged
merged 3 commits into from
May 28, 2019

Conversation

pakrym
Copy link
Contributor

@pakrym pakrym commented May 27, 2019

Also, adds validating stream that makes sure appropriate methods are being called on the response stream.

Fix bugs found using validating stream.

Breaking change:

Sync request factory is now required.

Before:

response.Value.Content = RetriableStream.Create(
    response.GetRawResponse(),
    async startOffset =>
        (await this.StartDownloadAsync(
            range,
            accessConditions,
            rangeGetContentHash,
            startOffset,
            cancellation)
            .ConfigureAwait(false))
        .GetRawResponse(),
    // TODO: For now we're using the default ResponseClassifier
    // on BlobConnectionOptions so we'll do the same here
    new ResponseClassifier(),
    Constants.MaxReliabilityRetries);

After:

response.Value.Content = RetriableStream.Create(
    response.GetRawResponse(),
        startOffset =>
        (this.StartDownloadAsync(
                range,
                accessConditions,
                rangeGetContentHash,
                startOffset,
                cancellation)
            .ConfigureAwait(false).GetAwaiter().GetResult())
        .GetRawResponse(),
    async startOffset =>
        (await this.StartDownloadAsync(
            range,
            accessConditions,
            rangeGetContentHash,
            startOffset,
            cancellation)
            .ConfigureAwait(false))
        .GetRawResponse(),
    // TODO: For now we're using the default ResponseClassifier
    // on BlobConnectionOptions so we'll do the same here
    new ResponseClassifier(),
    Constants.MaxReliabilityRetries);

}
else
{
s_eventSource.ResponseContentText(message.Response, responseTextEncoding, message.CancellationToken);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need these sync code paths? We are inside an async method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a pattern we use where we write a method that takes a bool async parameter and becomes fully sync or async depending on the value.

This is a shorter example from BearerTokenAuthenticationPolicy implementation:

        public override Task ProcessAsync(HttpPipelineMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
        {
            return ProcessAsync(message, pipeline, true);
        }

        public override void Process(HttpPipelineMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline)
        {
            ProcessAsync(message, pipeline, false).EnsureCompleted();
        }

        public async Task ProcessAsync(HttpPipelineMessage message, ReadOnlyMemory<HttpPipelinePolicy> pipeline, bool async)
        {
>>>>        string token = async ?
                    await _credential.GetTokenAsync(_scopes, message.CancellationToken).ConfigureAwait(false) :
                    _credential.GetToken(_scopes, message.CancellationToken);

            if (token != _currentToken)
            {
                // Avoid per request allocations
                _currentToken = token;
                _headerValue = "Bearer " + token;
            }

            message.Request.SetHeader(HttpHeader.Names.Authorization, _headerValue);

            if (async)
            {
>>>>            await ProcessNextAsync(message, pipeline);
            }
            else
            {
>>>>            ProcessNext(message, pipeline);
            }
        }

@pakrym pakrym requested a review from tg-msft May 28, 2019 17:38
@pakrym pakrym merged commit 1f0ed2e into Azure:master May 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants