Skip to content

Commit

Permalink
change lock release position and change taskcompletionsource options
Browse files Browse the repository at this point in the history
  • Loading branch information
aspriddell committed Nov 27, 2021
1 parent 57a7c41 commit d2503da
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions DragonFruit.Common.Data/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ protected virtual void SetupRequest(HttpRequestMessage request)
/// <param name="disposeResponse">Whether to dispose of the <see cref="HttpResponseMessage"/> produced after <see cref="processResult"/> has been invoked.</param>
protected Task<T> InternalPerform<T>(HttpRequestMessage request, Func<HttpResponseMessage, Task<T>> processResult, bool disposeResponse, CancellationToken token = default)
{
var monitor = new TaskCompletionSource<T>();
var (client, clientLock) = GetClient();
var monitor = new TaskCompletionSource<T>(TaskCreationOptions.RunContinuationsAsynchronously);

// post-modification
SetupRequest(request);
Expand All @@ -214,15 +214,8 @@ protected Task<T> InternalPerform<T>(HttpRequestMessage request, Func<HttpRespon
// ReSharper disable once MethodSupportsCancellation (we need to run regardless of cancellation to release lock)
client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token).ContinueWith(async t =>
{
// exit the read lock as soon as the request has been sent and processed
// this is because the callback could involve re-processing the request
clientLock.Dispose();

try
{
// do another cancellation token test
token.ThrowIfCancellationRequested();

// evaluate task status and update monitor
switch (t.Status)
{
Expand Down Expand Up @@ -251,6 +244,9 @@ protected Task<T> InternalPerform<T>(HttpRequestMessage request, Func<HttpRespon
{
t.Result.Dispose();
}

// exit the read lock after fully processing
clientLock.Dispose();
}
});

Expand Down

0 comments on commit d2503da

Please sign in to comment.