Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

通过transport.SetDial自定义dial来做到指定tcp连接使用指定ip后无法关闭连接。 #393

Open
wuchuwei opened this issue Oct 16, 2024 · 0 comments

Comments

@wuchuwei
Copy link

wuchuwei commented Oct 16, 2024

代码如下
` dia := func(ctx context.Context, network, addr string) (net.Conn, error) {
// Specify the custom IP address you want to connect to
dialer := &net.Dialer{
Timeout: 800 * time.Millisecond, // Set connection timeout
KeepAlive: -1,
// You can specify the local interface by setting LocalAddr here if needed
}
// Resolve and use the specified IP address (114.114.114.114 for this example)
ipAddr := ip+":443"
return dialer.DialContext(ctx, network, ipAddr)
}

// Create a req client with the custom transport.
client := req.C().SetTimeout(2000 * time.Millisecond).DisableKeepAlives().SetRedirectPolicy(req.NoRedirectPolicy()).SetTLSFingerprintChrome().SetUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
transport := client.GetTransport()
transport.MaxIdleConnsPerHost = -1
transport.DisableKeepAlives = true
    transport.SetDial(dia)

// Make the request
resp, err := client.R().EnableCloseConnection().Get(url)
     io.Copy(ioutil.Discard, resp.Body)
   resp.Body.Close()
   transport.CloseIdleConnections()

`
无论如何做都无法主动关闭tcp连接。
我这个功能主要是对网站进行dns优选。找出那个ip相应快。功能是做出来了。但代码长时间运行就会出现大量未关闭的连接。
要如何才能主动关闭连接?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant