如何使用 Pathoschild 库中的 SetRequestCoordinator?
How to use SetRequestCoordinator from Pathoschild lib?
我故意将超时设置得非常低,以使 http 调用崩溃以测试重试功能,但出现了异常,因此 httpClient 没有重试之前的请求。
我一开始以为只有在maxRetry配置的一些尝试失败后(HTTP代码500)才会引发异常。
所以我的代码逻辑被破坏了,如果我强制执行再次执行 PostAsync
的 API 调用,重试功能将毫无用处并且代码很脏。
IClient clientV2 = new FluentClient(apiClient.getBasedUrl());
clientV2.BaseClient.Timeout = TimeSpan.FromMilliseconds(50);
clientV2.SetRequestCoordinator(
maxRetries: 3,
shouldRetry: request => request.StatusCode != HttpStatusCode.OK,
getDelay: (attempt, response) => { return TimeSpan.FromSeconds(1); } // 1, 2, and 3 seconds
);
try
{
T result = clientV2.PostAsync(_RelativeURL)
.WithHeader("APIKEY", apikey)
.WithHeader("PAYLOAD", b64)
.WithHeader("SIGNATURE", hmacSig)
.As<T>()
.Result;
dynamicJSONResult = bitfinexTicker;
} catch (Exception ex)
{
Console.WriteLine(ex);
}
System.AggregateException: One or more errors occurred. --->
System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task 1.get_Result()
at xxx.xx.x.x.QueryPrivate[T](ApiClient apiClient, String myHTTPMethod, String _RelativeURL, Int32 _myTimeOut, Object _payload) in zzz.cs:line 337
这是 3.0 中添加的重试协调器的一个疏忽——如果服务器 returns 出现错误,它会重试,但如果请求在本地超时则不会。这在 3.1 中已修复。
我故意将超时设置得非常低,以使 http 调用崩溃以测试重试功能,但出现了异常,因此 httpClient 没有重试之前的请求。
我一开始以为只有在maxRetry配置的一些尝试失败后(HTTP代码500)才会引发异常。
所以我的代码逻辑被破坏了,如果我强制执行再次执行 PostAsync
的 API 调用,重试功能将毫无用处并且代码很脏。
IClient clientV2 = new FluentClient(apiClient.getBasedUrl());
clientV2.BaseClient.Timeout = TimeSpan.FromMilliseconds(50);
clientV2.SetRequestCoordinator(
maxRetries: 3,
shouldRetry: request => request.StatusCode != HttpStatusCode.OK,
getDelay: (attempt, response) => { return TimeSpan.FromSeconds(1); } // 1, 2, and 3 seconds
);
try
{
T result = clientV2.PostAsync(_RelativeURL)
.WithHeader("APIKEY", apikey)
.WithHeader("PAYLOAD", b64)
.WithHeader("SIGNATURE", hmacSig)
.As<T>()
.Result;
dynamicJSONResult = bitfinexTicker;
} catch (Exception ex)
{
Console.WriteLine(ex);
}
System.AggregateException: One or more errors occurred. --->
System.Threading.Tasks.TaskCanceledException: A task was canceled.
--- End of inner exception stack trace ---
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
at System.Threading.Tasks.Task 1.GetResultCore(Boolean waitCompletionNotification)
at System.Threading.Tasks.Task 1.get_Result()
at xxx.xx.x.x.QueryPrivate[T](ApiClient apiClient, String myHTTPMethod, String _RelativeURL, Int32 _myTimeOut, Object _payload) in zzz.cs:line 337
这是 3.0 中添加的重试协调器的一个疏忽——如果服务器 returns 出现错误,它会重试,但如果请求在本地超时则不会。这在 3.1 中已修复。