将Polly的HTTP请求转换为F#

3

我正在使用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]);
}
2个回答

3

您可以尝试使用 Result<'TOk,'TError>Async<T> 更加功能化的方法。

open System.Net
open System

type Request =
    { Url      : string
      Proxies  : string list
      UserName : string
      Password : string }

let requestWithRetry request =
    let client = 
        new WebClient (
            Credentials = new NetworkCredential(
                request.UserName,
                request.Password))
    let uri = Uri request.Url
    let rec retry = function
        | [] -> Error "Exhausted proxies" |> async.Return
        | (proxy:string)::rest -> async {
            try 
                do client.Proxy <- new WebProxy(proxy, UseDefaultCredentials = true)
                let! response = client.AsyncDownloadString uri
                return Ok (response, proxy)
            with _ ->
                do! Async.Sleep 1000
                return! retry rest
        }
    retry request.Proxies

我喜欢你的答案里的一切,干得好!不幸的是,AsyncDownloadString 没有被包括到 .NET Core 中。 - Brett Rowberry
我认为 let! response = client.AsyncDownloadString uri 等同于 let! response = { return client.DownloadString uri },只有第二个适用于 .NET Core。 - Brett Rowberry
@BrettRowberry,这不一样。不要那样做:D 这可能会阻塞线程池中的每个线程,而不是重用它们!您可以使用与IEvent的Interop - Async.AwaitEvent。或者简单地使用Async.AwaitTask - Szer

0

我成功地完成了这个翻译。虽然在这个过程中我学到了更多有关 F# 和 Action,但是我其实并不是很喜欢它。

type Result = { Value: string; Proxy: string }

let request (proxies:string[]) (username:string) (password:string) (url:string) : Result =                   
    if (proxies = null) then raise <| new ArgumentNullException()

    use client = new WebClient()
    client.Credentials <- NetworkCredential(username, password)
    let mutable result = String.Empty;
    let mutable proxyIndex = 0;

    let policy =
        Policy
            .Handle<Exception>()
            .WaitAndRetry(
                sleepDurations = [| TimeSpan.FromSeconds(1.0) |], 
                onRetry = Action<Exception, TimeSpan>(fun _ _ -> proxyIndex <- proxyIndex + 1)
                )

    let makeCall () =
        if (proxyIndex >= proxies.Length) 
        then failwith ("Exhausted proxies: " + String.Join(", ", proxies))
        else
            let proxy = WebProxy(proxies.[proxyIndex])
            proxy.UseDefaultCredentials <- true
            client.Proxy <- proxy
            result <- client.DownloadString(new Uri(url));

    policy.Execute(Action makeCall)

    { Result.Value = result; Proxy = proxies.[proxyIndex]}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接