我知道我可以指定 HTTP 错误代码列表(例如 408、502、503 等),我想使用Polly重试,但是如果未指定,默认情况下将重试的这些代码的列表是什么?
我有一个看起来像这样的政策
var retryPolicy = Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.Unauthorized)
.WaitAndRetryAsync(3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (resp, timeSpan, context) =>
{
// not sure what to put here
});
Run Code Online (Sandbox Code Playgroud)
然后我有一个指定的客户端,看起来像这样
services.AddHttpClient("MyClient", client =>
{
client.BaseAddress = new Uri("http://some-url.com");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
client.Timeout = 30000;
})
.AddPolicyHandler(retryPolicy);
Run Code Online (Sandbox Code Playgroud)
如果收到 401,我需要刷新 http 客户端上的不记名令牌。因此,在完美的世界中,以下代码将完全实现我想要完成的任务
var retryPolicy = Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(resp => resp.StatusCode == HttpStatusCode.Unauthorized)
.WaitAndRetryAsync(3,
retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (resp, timeSpan, context) =>
{
var newToken = GetNewToken();
//httpClient doesn't …
Run Code Online (Sandbox Code Playgroud) 我有一个应用程序请求经过身份验证的服务,需要通过access_token
.
我的想法是在过期时使用 Polly 重试access_token
。
我在 .NET Core 3.1 应用程序中使用 Refit (v5.1.67) 和 Polly (v7.2.1)。
服务注册如下:
services.AddTransient<ExampleDelegatingHandler>();
IAsyncPolicy<HttpResponseMessage> retryPolicy = Policy<HttpResponseMessage>
.Handle<ApiException>()
.RetryAsync(1, (response, retryCount) =>
{
System.Diagnostics.Debug.WriteLine($"Polly Retry => Count: {retryCount}");
});
services.AddRefitClient<TwitterApi>()
.ConfigureHttpClient(c =>
{
c.BaseAddress = new Uri("https://api.twitter.com/");
})
.AddHttpMessageHandler<ExampleDelegatingHandler>()
.AddPolicyHandler((sp, req) =>
{
//this policy does not works, because the exception is not catched on
//"Microsoft.Extensions.Http.PolicyHttpMessageHandler" (DelegatingHandler)
return retryPolicy;
});
Run Code Online (Sandbox Code Playgroud)
public interface TwitterApi
{
[Get("/2/users")]
Task<string> GetUsers();
}
Run Code Online (Sandbox Code Playgroud)
public class ExampleDelegatingHandler : DelegatingHandler …
Run Code Online (Sandbox Code Playgroud) 我想在我们的.net应用程序中引入瞬态故障处理.我看到现在有两个nu-get包.一个是Polly框架,另一个是Microsoft瞬态故障处理应用程序块.
我们调查并看到了支持异步功能和不同的重试策略.Polly框架还支持断路器.有人建议使用哪个框架有一些可靠的原因.此外,如果框架的利弊以及互联网社区的支持和未来扩展和支持的范围可以突出显示,那将是非常有用的.提前致谢.
我正在尝试为 polly 编写单元测试,但看起来返回已被缓存。
方法 PostAsyncWithRetry:
using Polly;
using System;
using System.Diagnostics;
using System.Net.Cache;
using System.Net.Http;
public class RetryClient
{
private HttpClient httpClient = new HttpClient(new WebRequestHandler()
{ CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore) });
public HttpResponseMessage PostAsyncWithRetry(
String url,
String path,
StringContent httpContent)
{
httpClient.BaseAddress = new Uri(url);
var retryPolicy =
Policy.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => !r.IsSuccessStatusCode)
.RetryAsync(3, (exception, retryCount, context) =>
{
Debug.WriteLine("RetryCount: {0}", retryCount);
});
var response = retryPolicy.ExecuteAsync(async () =>
{
return await httpClient.PostAsync(path, httpContent);
}
);
return response.Result;
}
} …
Run Code Online (Sandbox Code Playgroud) 或者:如何从静态方法记录。
从https://github.com/App-vNext/Polly你可以看到像这样的例子,其中记录器神奇地可用:
Policy
.Timeout(30, onTimeout: (context, timespan, task) =>
{
logger.Warn($"{context.PolicyKey} at {context.ExecutionKey}: execution timed out after {timespan.TotalSeconds} seconds.");
});
Run Code Online (Sandbox Code Playgroud)
在我的代码中,我使用IHttpClientFactory
dotnet core 2.1 中的新模式,并将其添加到我的 Startup.csConfigureServices
方法中:
services.AddHttpClient<IMySuperHttpClient, MySuperHttpClient>()
.AddPolicyHandler(MySuperHttpClient.GetRetryPolicy())
.AddPolicyHandler(MySuperHttpClient.GetCircuitBreakerPolicy());
Run Code Online (Sandbox Code Playgroud)
静态且GetRetryPolicy
看起来像这样:
internal static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.NotFound)
.WaitAndRetryAsync(
retryCount: 4,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: OnRetry);
}
Run Code Online (Sandbox Code Playgroud)
其中OnRetry
方法也必须是静态的:
private static void OnRetry(DelegateResult<HttpResponseMessage> delegateResult, TimeSpan timespan, Context context)
{
// var logger = ?? …
Run Code Online (Sandbox Code Playgroud) 我正在调用外部API并希望处理调用返回的事件Unauthorized
HttpResponseMessage
.发生这种情况时,我想刷新访问令牌并再次拨打电话.
我正在尝试使用Polly
以下代码:
public async Task<HttpResponseMessage> MakeGetRequestAsync()
{
var retryPolicy = Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized)
.Retry(1, (exception, retryCount) =>
{
RefreshAccessToken();
});
var result = await retryPolicy.ExecuteAsync(() => CallApiAsync());
return result;
}
private async Task<HttpResponseMessage> CallApiAsync()
{
var url = Options.ResourceSandboxUrl;
var httpClient = new HttpClient();
SetRequestHeaders(httpClient);
var response = await httpClient.GetAsync(url);
response.StatusCode = HttpStatusCode.Unauthorized;
return response;
}
Run Code Online (Sandbox Code Playgroud)
我在
ExecuteAsync
语句中添加了断点,并且DoSomethingAsync
- 当我跳过时ExecuteAsync
DoSomethingAsync
没有调用并且控制返回到调用的函数MakeGetRequestAsync
我不明白为什么DoSomethingAsync
不被召唤 - 任何人都可以帮助我实现我想要实现的目标吗?
我已经看过关于Stack …
我正在使用一个非常脆弱的 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 实现重试逻辑,以在有一定延迟的情况下永远重试执行函数,但不处理异常。该场景是重复获取状态信息但没有预期的异常。
我正在尝试使timout政策正常运行。集成api时,我有以下要求。
对于此任务,我想使用Polly,在我看来这是一个很棒的组件,可帮助处理瞬态故障。但是,由于我对这项技术还很陌生,所以我只想确定自己是否正确实施。
首先,我像这样用Polly创建了一个超时策略
var timeoutPolicy =
Policy.TimeoutAsync(
TimeSpan.FromSeconds( 20 ),
TimeoutStrategy.Optimistic,
async ( context, timespan, task ) => {
//write here the cancel request
} );
Run Code Online (Sandbox Code Playgroud)
然后,我准备执行该策略
var policyResult = await timeoutPolicy.ExecuteAndCaptureAsync( async () => {
//make here the request 1
} );
Run Code Online (Sandbox Code Playgroud)
我从文档中得到的是,如果在timeoutPolicy.ExecuteAndCaptureAsync
委托内部发生了超时,则Polly会自动调用onTimeout
委托。对?
但是我的问题是:
polly ×10
c# ×9
.net ×2
.net-core ×2
asp.net-mvc ×1
logging ×1
moq ×1
refit ×1
retry-logic ×1
retrypolicy ×1
unit-testing ×1