tls-client icon indicating copy to clipboard operation
tls-client copied to clipboard

[Bug]: roundtripper and client not reuse tcp connection

Open PandaWorker opened this issue 10 months ago • 1 comments

TLS client version

1.7.4

System information

...

Issue description

roundtripper does not support reusing tcp connection for subsequent requests

For each RoundTrip, a new connection opens and a handshake takes place, which greatly affects the speed of sending requests

Steps to reproduce / Code Sample

package main

import (
	"context"
	"io"
	"log"
	"net/url"

	http "github.com/bogdanfinn/fhttp"
	client "github.com/bogdanfinn/tls-client"
)

func main() {

	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()

	options := []client.HttpClientOption{
		//client.WithForceHttp1(),
		client.WithRandomTLSExtensionOrder(),
		client.WithTransportOptions(&client.TransportOptions{
			MaxConnsPerHost:    1,
			DisableCompression: true,
		}),
	}
	client, _ := client.NewHttpClient(client.NewNoopLogger(), options...)

	u, _ := url.ParseRequestURI("https://tls.peet.ws/api/tls")

	for i := 0; i < 3; i++ {
		request := &http.Request{
			Method: "GET",
			URL:    u,
			Header: http.Header{},
		}
		request = request.WithContext(ctx)

		response, _ := client.Do(request)

		log.Print(response)
		body, _ := io.ReadAll(response.Body)
		log.Printf("%s", string(body))
	}
}

PandaWorker avatar Apr 26 '24 09:04 PandaWorker

https://stackoverflow.com/questions/33238518/what-could-happen-if-i-dont-close-response-body

roachadam avatar Apr 26 '24 17:04 roachadam