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

http requests to keep using the same TCP connection #349

Closed
valdemarpavesi opened this issue Jun 28, 2024 · 5 comments · Fixed by #351
Closed

http requests to keep using the same TCP connection #349

valdemarpavesi opened this issue Jun 28, 2024 · 5 comments · Fixed by #351

Comments

@valdemarpavesi
Copy link

using context and setting IdleConnTimeout and DisableKeepAlives and req.Close = false

still release the TCP for every new request.

We wanna keep the same TCP connection
Have you accomplished it?

        ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
	defer cancel() // Ensure the context is canceled to free resources

	defaultTransport := http.DefaultTransport.(*http.Transport)
	transport := &http.Transport{
		Proxy: defaultTransport.Proxy,
		//DialContext:           defaultTransport.DialContext,
		DialContext: (&net.Dialer{
			Timeout:   60 * time.Second,
			KeepAlive: 15 * time.Second,
			DualStack: true,
		}).DialContext,
		MaxIdleConns:          defaultTransport.MaxIdleConns,
		IdleConnTimeout:       10 * time.Minute, //defaultTransport.IdleConnTimeout
		ExpectContinueTimeout: defaultTransport.ExpectContinueTimeout,
		TLSHandshakeTimeout:   30 * time.Second,
		DisableKeepAlives:     false, // default true, if true, disables HTTP keep-alives and will only use the connection to the server for a single HTTP request, tcp rst for every send.
		TLSClientConfig: &tls.Config{
			InsecureSkipVerify: true,
		},
	}

	client_HTTPClient := &http.Client{Transport: transport}

	config := gofish.ClientConfig{
		Endpoint:   "test",
		Username:   "test",
		Password:   "test",
		Insecure:   true,
		HTTPClient: client_HTTPClient,
	}

	c, err := gofish.ConnectContext(ctx, config)
	if err != nil {
		panic(err)
	}

	service := c.Service
	chassis, err := service.Chassis()
	if err != nil {
		panic(err)
	}

	for _, chass := range chassis {
		fmt.Printf("Chassis: %#v\n\n", chass)
	}

	// The client and its underlying connections can be reused for subsequent requests
	// The connections will be kept open and reused according to the transport's configuration
	fmt.Println("Press any key to continue...")
	var input string
	fmt.Scanln(&input)
@stmcginnis
Copy link
Owner

I have not tried this, but you could make a local modification and see if it would work. I think you would just need to change this line:

https://github.com/stmcginnis/gofish/blob/main/client.go#L503

@valdemarpavesi
Copy link
Author

valdemarpavesi commented Jul 1, 2024

Thanks @stmcginnis, yes it helps

   transport
   DisableKeepAlives:     false
   IdleConnTimeout:       1 * time.Minute

  and
  
req.Close = false
req.Header.Add("Connection", "keep-alive")

vendor1 tcp fin/ack 11 times
vendor2 tcp fin/ack 37

with req.Close = true

vendor1 tcp fin/ack 169 times
vendor2 tcp fin/ack 37

vendor2 make no difference.

Thanks.

regards!
Valdemar

@stmcginnis
Copy link
Owner

Awesome - does this mean it works how you would like it to with that change? I'm wondering if we need to expose a config flag or something to make this easier. Or if there is value in trying to make this the default behavior.

@valdemarpavesi
Copy link
Author

Hello @stmcginnis
Yes, if can expose it into the config will be better.

regards!
Valdemar

@stmcginnis
Copy link
Owner

If you're able to test with #351, that would be great to have someone else verify it improves things.

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

Successfully merging a pull request may close this issue.

2 participants