valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.91k stars 1.76k forks source link

【question】How can I reuse the client and use proxy? #1904

Open CsVeryLoveXieWenLi opened 5 days ago

CsVeryLoveXieWenLi commented 5 days ago

Sorry, I don't speak English. So the English is all machine translated.

1. 源代码 || Source Code

我尝试复用*fasthttp.Client,而且使用代理。 I tried to reuse *fasthttp.Client and use a proxy.

预想的情况是代理URL是错误的,所以进行请求肯定会出现错误。 The expected situation is that the proxy URL is wrong, so the request will definitely fail.

package main

import (
    "fmt"
    "time"

    "github.com/valyala/fasthttp"
    "github.com/valyala/fasthttp/fasthttpproxy"
)

func main() {
    // init
    client := &fasthttp.Client{
        ReadTimeout:  time.Millisecond * 688,
        WriteTimeout: time.Millisecond * 688,

        MaxIdleConnDuration: time.Second * 7,

        NoDefaultUserAgentHeader: false,

        Dial: fasthttpproxy.FasthttpHTTPDialerTimeout("1111", time.Second*2),
    }

    request := fasthttp.AcquireRequest()
    response := fasthttp.AcquireResponse()

    request.SetRequestURI("https://httpbin.org/get")

    // send proxy 1
    err := client.Do(request, response)
    fmt.Println(err)

    // send proxy 2
    client.Dial = fasthttpproxy.FasthttpHTTPDialerTimeout("2222", time.Second*2)

    err = client.Do(request, response)
    fmt.Println(err)
}

2. 结果 || Result

确实如此,代理URL是错误的。可是第二次请求设置了2222代理。然而,从错误输出来看,第二次请求并没有使用。 Indeed, the proxy URL is wrong. But the second request sets the 2222 proxy. However, from the error output, the second request does not use it.

go run Main.go

address 1111: missing port in address
address 1111: missing port in address

我尝试了另一种方式,第一次请求不设置代理,也就是fasthttp.Client初始化时。但是,第二次请求仍然不会使用2222代理。 I tried another way, not setting the proxy for the first request, which is when fasthttp.Client is initialized. However, the second request still does not use the 2222 proxy.

// init
client := &fasthttp.Client{
ReadTimeout:  time.Millisecond * 688,
WriteTimeout: time.Millisecond * 688,

MaxIdleConnDuration: time.Second * 7,

NoDefaultUserAgentHeader: false,

// Dial: fasthttpproxy.FasthttpHTTPDialerTimeout("1111", time.Second*2),
}
go run Main.go

<nil>
<nil>

3. 疑问 || Question

难道无法重新设置Dial的值吗,只有在初始化时才可以设置。 Is it not possible to reset the value of Dial? It can only be set during initialization.

我该如何重用*fasthttp.Client,并重新设置代理? How ​​can I reuse *fasthttp.Client and re-set the proxy?

CsVeryLoveXieWenLi commented 4 days ago

不应修改? The fields of a Client should not be changed while it is in use.

newacorn commented 3 days ago

不应修改? The fields of a Client should not be changed while it is in use.

When a Client instance makes its first request to a domain, a HostClient instance is created for that domain, and the initial information for the HostClient instance is taken from the Client's configuration. Subsequent requests to the same domain will use the cached HostClient instance, and the Client's configuration will not be read again. If you want to update the configuration of the existing Client, you need to create a new Client instance. This is how it currently works.

newacorn commented 3 days ago

Perhaps a method could be added to Client to clear all cached HostClient instances. However, in that case, reusing the Client wouldn’t have much purpose. You could also use HostClient directly, but it doesn’t support cross-domain requests or protocol redirection.

CsVeryLoveXieWenLi commented 3 days ago

Perhaps a method could be added to Client to clear all cached HostClient instances. However, in that case, reusing the Client wouldn’t have much purpose. You could also use HostClient directly, but it doesn’t support cross-domain requests or protocol redirection.

Thank you very much for your answer.

HostClient的API与Client的API不是没什么区别吗?难道HostClient实例允许更改属性? Isn’t there any difference between the API of HostClient and that of Client? Does the HostClient instance allow changing properties?