我正在向 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]);
}