bogdanfinn / tls-client

net/http.Client like HTTP Client with options to select specific client TLS Fingerprints to use for requests.
BSD 4-Clause "Original" or "Old" License
667 stars 133 forks source link

[Bug]: roundtripper and client not reuse tcp connection #112

Open PandaWorker opened 2 months ago

PandaWorker commented 2 months ago

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))
    }
}
roachadam commented 2 months ago

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