Jes*_*olm 2 c# castle-windsor throttling polly
我们正在努力应对 429 HTTP 异常(来自 SharePoint Online 或 Microsoft Graph),我想利用 Polly 和 Castle.Windsor 来处理这个问题。
我的代码是(摘录)
在我的 Castle.Windsor 容器中注册 Polly 的东西:
_container.Register(Component.For<IRepository>()
.ImplementedBy<Repository>()
.DependsOn(Dependency.OnValue<ImportSettings>(_importSettings))
.Interceptors(InterceptorReference.ForKey("throttle")).Anywhere
.LifestyleTransient());
_container.Register(Component.For<WaitAndRetryInterceptor<WebException>>().LifeStyle.Singleton
.Named("throttle"));
Run Code Online (Sandbox Code Playgroud)
我的波莉的东西:
public class WaitAndRetryInterceptor<T> : IInterceptor where T : WebException
{
private readonly RetryPolicy _policy;
public void Intercept(IInvocation invocation)
{
var dictionary = new Dictionary<string, object> {{"methodName", invocation.Method.Name}};
_policy.Execute(invocation.Proceed, dictionary);
}
public WaitAndRetryInterceptor()
{
_policy =
Policy
.Handle<T>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(16), TimeSpan.FromSeconds(32), TimeSpan.FromSeconds(64),
TimeSpan.FromSeconds(128), TimeSpan.FromSeconds(256), TimeSpan.FromSeconds(512)
});
}
}
Run Code Online (Sandbox Code Playgroud)
所以这个实现满足了我的需求 - 但它非常保守。因此,我尝试实现对抛出的 429 异常的直接支持 - 特别是对Reply-After服务器可用标头的支持。
我从此https://github.com/App-vNext/Polly/issues/414发现,我需要实现对采用 a 的重载之一的支持sleepDurationProvider,但我在正确获取代码时遇到问题。
我的实现是这样的:
_policy =
Policy
.Handle<HttpRequestException>()
.OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Accepted) //needs to be changed to 429
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: (retryCount, response) =>
{
return getServerWaitDuration(response);
})
;
Run Code Online (Sandbox Code Playgroud)
只是getServerWaitDuration返回一个TimeSpan
private TimeSpan getServerWaitDuration(DelegateResult<HttpResponseMessage> response)
{
return TimeSpan.Zero; //not actual code ;-)
}
Run Code Online (Sandbox Code Playgroud)
我的想法是,我将简单地查看服务器响应的标头并将时间跨度传递回sleepDurationProvider.
但是 - 我从配置sleepDurationProvider. 我被告知这(retryCount, response)是一个“不兼容的匿名函数签名”
我觉得我在这里遗漏了一些明显的东西。但为什么?如何访问该response对象以提取Retry-After持续时间?
您可能会像这样查看标题
public AsyncRetryPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => msg.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
.OrResult(r => r?.Headers?.RetryAfter != null)
.WaitAndRetryAsync(
3,
sleepDurationProvider: (retryCount, response, context) =>
{
return response.Result.Headers.RetryAfter.Delta.Value;
},
onRetryAsync: (e, ts, i, ctx) => Task.CompletedTask
);
}
Run Code Online (Sandbox Code Playgroud)