我正在使用Polly进行HTTP请求。我想要在一个数组中的每个代理上等待1秒后重试一次。
有什么更好的方法吗?
如何在F#中实现这个功能?
public static Result RequestWithRetry(string url, string[] proxies, string username, string password)
{
if (proxies == null) throw new ArgumentNullException("null proxies array");
var client = new WebClient { Credentials = new NetworkCredential(username, password) };
var result = String.Empty;
var proxyIndex = 0;
var policy = Policy
.Handle<Exception>()
.WaitAndRetry(new[]
{
TimeSpan.FromSeconds(1)
}, (exception, timeSpan) => proxyIndex++);
policy.Execute(() =>
{
if (proxyIndex >= proxies?.Length) throw new Exception($"Exhausted proxies: {String.Join(", ", proxies)}");
client.Proxy = new WebProxy(proxies?[proxyIndex]) { UseDefaultCredentials = true };
result = client.DownloadString(new Uri(url));
});
return new Result(value: result, proxy: proxies[proxyIndex]);
}
let! response = client.AsyncDownloadString uri
等同于let! response = { return client.DownloadString uri }
,只有第二个适用于 .NET Core。 - Brett RowberryIEvent
的Interop -Async.AwaitEvent
。或者简单地使用Async.AwaitTask
。 - Szer