Skip to content

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

@CsVeryLoveXieWenLi

Description

@CsVeryLoveXieWenLi

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions