Skip to content

Avoiding issues related to StreamContent in HttpClient #112

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 22 additions & 43 deletions dropbox-sdk-dotnet/Dropbox.Api/DropboxRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async Task<TResponse> ITransport.SendRpcRequestAsync<TRequest, TResponse, TError
/// </exception>
async Task<TResponse> ITransport.SendUploadRequestAsync<TRequest, TResponse, TError>(
TRequest request,
Stream body,
IEnumerable<byte> body,
string host,
string route,
string auth,
Expand Down Expand Up @@ -278,7 +278,7 @@ private async Task<Result> RequestJsonStringWithRetry(
string auth,
RouteStyle routeStyle,
string requestArg,
Stream body = null)
IEnumerable<byte> body = null)
{
var attempt = 0;
var maxRetries = this.options.MaxClientRetries;
Expand All @@ -290,57 +290,36 @@ private async Task<Result> RequestJsonStringWithRetry(
{
throw new ArgumentNullException("body");
}

// to support retry logic, the body stream must be seekable
// if it isn't we won't retry
if (!body.CanSeek)
{
maxRetries = 0;
}
}

try
while (true)
{
while (true)
try
{
try
{
return await this.RequestJsonString(host, routeName, auth, routeStyle, requestArg, body)
.ConfigureAwait(false);
}
catch (RateLimitException)
return await this.RequestJsonString(host, routeName, auth, routeStyle, requestArg, body)
.ConfigureAwait(false);
}
catch (RateLimitException)
{
throw;
}
catch (RetryException)
{
// dropbox maps 503 - ServiceUnavailable to be a rate limiting error.
// do not count a rate limiting error as an attempt
if (++attempt > maxRetries)
{
throw;
}
catch (RetryException)
{
// dropbox maps 503 - ServiceUnavailable to be a rate limiting error.
// do not count a rate limiting error as an attempt
if (++attempt > maxRetries)
{
throw;
}
}
}

// use exponential backoff
var backoff = TimeSpan.FromSeconds(Math.Pow(2, attempt) * r.NextDouble());
// use exponential backoff
var backoff = TimeSpan.FromSeconds(Math.Pow(2, attempt) * r.NextDouble());
#if PORTABLE40
await TaskEx.Delay(backoff);
#else
await Task.Delay(backoff).ConfigureAwait(false);
await Task.Delay(backoff).ConfigureAwait(false);
#endif
if (body != null)
{
body.Position = 0;
}
}
}
finally
{
if (body != null)
{
body.Dispose();
}
}
}

Expand Down Expand Up @@ -386,7 +365,7 @@ private async Task<Result> RequestJsonString(
string auth,
RouteStyle routeStyle,
string requestArg,
Stream body = null)
IEnumerable<byte> body = null)
{
var hostname = this.options.HostMap[host];
var uri = this.GetRouteUri(hostname, routeName);
Expand Down Expand Up @@ -451,7 +430,7 @@ private async Task<Result> RequestJsonString(
throw new ArgumentNullException("body");
}

request.Content = new CustomStreamContent(body);
request.Content = new ByteArrayContent(body.ToArray());
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
break;
default:
Expand Down
Loading