tra*_*max 9 c# retrypolicy polly
我正在使用一个非常脆弱的 API。有时我500 Server Error用Timeout,其他时间我还可以得到500 Server Error,因为我给它输入,它不能处理SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.。
这两种情况都给了我,HttpRequestException但我可以查看来自服务器的回复消息并确定异常的原因。如果是超时错误,我应该再试一次。如果这是一个错误的输入,我应该重新抛出异常,因为再多的重试也无法解决错误数据的问题。
我想对 Polly 做的是在尝试重试之前检查响应消息。但是到目前为止我看到的所有样本都只包含异常类型。
到目前为止,我想出了这个:
HttpResponseMessage response = null;
String stringContent = null;
Policy.Handle<FlakyApiException>()
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
async (exception, timeSpan, context) =>
{
response = await client.PostAsync(requestUri, new StringContent(serialisedParameters, Encoding.UTF8, "application/json"));
stringContent = await response.Content.ReadAsStringAsync();
if (response.StatusCode == HttpStatusCode.InternalServerError && stringContent.Contains("Timeout"))
{
throw new FlakyApiException(stringContent);
}
});
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来进行这种检查?
通常,您可以配置 Polly 策略以响应执行结果(不仅仅是异常),例如HttpResponseMessage.StatusCode使用谓词检查 an 。Polly 自述文件中的示例。
然而,没有内置的方法来配置单个Polly 策略来额外响应响应消息的内容。这是因为(如您的示例所示)获取该内容需要第二次异步调用,这本身可能会引发网络错误。
这个tl;dr产生了关于如何表达(以简单的语法)单个策略的复杂性,该策略管理两个不同的异步步骤,每个步骤可能有不同的错误处理。之前在 Polly github 上的相关讨论:欢迎评论。
因此,当一个序列需要两个单独的异步调用时,Polly 团队目前建议将其表示为两个单独的策略,类似于本答案末尾的示例。
您问题中的特定示例可能不起作用,因为onRetryAsync委托 (throwing FlakyApiException) 本身不受策略保护。策略仅保护通过 执行的委托的执行.Execute/ExecuteAsync(...)。
一种方法可能是使用两种策略,重试策略重试所有典型的 http 异常和状态代码,包括 500;然后在里面一个 Polly FallbackPolicy,它捕获状态代码 500 表示SqlDateTime overflow,并通过重新抛出作为某些可区分的异常 ( CustomSqlDateOverflowException)来排除重试。
IAsyncPolicy<HttpResponseMessage> rejectSqlError = Policy<HttpResponseMessage>
.HandleResult(r => r.StatusCode == HttpStatusCode.InternalServerError)
.FallbackAsync(async (delegateOutcome, context, token) =>
{
String stringContent = await delegateOutcome.Result.Content.ReadAsStringAsync(); // Could wrap this line in an additional policy as desired.
if (delegateOutcome.Result.StatusCode == HttpStatusCode.InternalServerError && stringContent.Contains("SqlDateTime overflow"))
{
throw new CustomSqlDateOverflowException(); // Replace 500 SqlDateTime overflow with something else.
}
else
{
return delegateOutcome.Result; // render all other 500s as they were
}
}, async (delegateOutcome, context) => { /* log (if desired) that InternalServerError was checked for what kind */ });
IAsyncPolicy<HttpResponseMessage> retryPolicy = Policy<HttpResponseMessage>
.Handle<HttpRequestException>()
.OrResult(r => r.StatusCode == HttpStatusCode.InternalServerError)
.OrResult(r => /* condition for any other errors you want to handle */)
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
async (exception, timeSpan, context) =>
{
/* log (if desired) retry being invoked */
});
HttpResponseMessage response = await retryPolicy.WrapAsync(rejectSqlError)
.ExecuteAsync(() => client.PostAsync(requestUri, new StringContent(serialisedParameters, Encoding.UTF8, "application/json"), cancellationToken));
Run Code Online (Sandbox Code Playgroud)