Skip to content

Commit

Permalink
Use asynchronous json deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
bbepis committed Jun 18, 2020
1 parent ce83c64 commit a04ddb0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Hayden/Api/NetworkPolicies.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class NetworkPolicies
+ TimeSpan.FromMilliseconds(random.Next(0, 5000)) // plus some jitter: up to 5 seconds
)
.WrapAsync(
Policy.TimeoutAsync(10, TimeoutStrategy.Pessimistic, async (context, span, failedTask) =>
Policy.TimeoutAsync(10, TimeoutStrategy.Optimistic, async (context, span, failedTask) =>
{
Program.Log($"Timeout occurred: {context.OperationKey}", true);
})
Expand All @@ -48,7 +48,7 @@ public static AsyncPolicy<T> GenericRetryPolicy<T>(int tries)
+ TimeSpan.FromMilliseconds(random.Next(0, 5000)) // plus some jitter: up to 5 seconds
)
.WrapAsync(
Policy.TimeoutAsync(10, TimeoutStrategy.Pessimistic, async (context, span, failedTask) =>
Policy.TimeoutAsync(10, TimeoutStrategy.Optimistic, async (context, span, failedTask) =>
{
Program.Log($"Timeout occurred: {context.OperationKey}", true);
})
Expand Down
6 changes: 2 additions & 4 deletions Hayden/Api/YotsubaApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Hayden.Api;
using Hayden.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Polly;
using Thread = Hayden.Models.Thread;

Expand Down Expand Up @@ -78,9 +79,6 @@ private static async Task<HttpResponseMessage> DoCall(Uri uri, HttpClient client
return await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken);
}

// For performance, we keep a single static instance of a JsonSerializer object instead of recreating it multiple times.
private static readonly JsonSerializer jsonSerializer = JsonSerializer.Create();

private static async Task<ApiResponse<T>> MakeYotsubaApiCall<T>(Uri uri, HttpClient client, DateTimeOffset? modifiedSince = null, CancellationToken cancellationToken = default)
{
int callCount = 0;
Expand All @@ -103,7 +101,7 @@ private static async Task<ApiResponse<T>> MakeYotsubaApiCall<T>(Uri uri, HttpCli
using StreamReader streamReader = new StreamReader(responseStream);
using JsonReader reader = new JsonTextReader(streamReader);

var obj = jsonSerializer.Deserialize<T>(reader);
var obj = (await JToken.LoadAsync(reader, cancellationToken)).ToObject<T>();

return new ApiResponse<T>(ResponseType.Ok, obj);
}
Expand Down

0 comments on commit a04ddb0

Please sign in to comment.