diff --git a/util/http_client_geo.go b/util/http_client_geo.go index 6769f25..614580e 100644 --- a/util/http_client_geo.go +++ b/util/http_client_geo.go @@ -9,7 +9,10 @@ import ( "time" ) -const geoHTTPTransportPoolMaxSize = 32 +const ( + geoHTTPTransportPoolMaxSize = 32 + geoHTTPDefaultMaxIdleConnsPerHost = 32 +) var ( defaultGeoHTTPPolicy = NewGeoDNSPolicy("", true) @@ -112,6 +115,9 @@ func newGeoHTTPTransportWithLookup(policy GeoDNSPolicy, lookup geoHostLookupFunc if base, ok := http.DefaultTransport.(*http.Transport); ok && base != nil { transport = base.Clone() } + if transport.MaxIdleConnsPerHost == 0 { + transport.MaxIdleConnsPerHost = geoHTTPDefaultMaxIdleConnsPerHost + } policy = NewGeoDNSPolicy(policy.Resolver(), policy.Fallback()) dialer := &net.Dialer{KeepAlive: 30 * time.Second} diff --git a/util/http_client_geo_test.go b/util/http_client_geo_test.go index 5b5832b..2371077 100644 --- a/util/http_client_geo_test.go +++ b/util/http_client_geo_test.go @@ -147,6 +147,17 @@ func TestNewGeoHTTPTransportClonesDefaultTransportSettings(t *testing.T) { } } +func TestNewGeoHTTPTransportExpandsImplicitIdlePoolForSharedUse(t *testing.T) { + original := http.DefaultTransport + defer func() { http.DefaultTransport = original }() + http.DefaultTransport = &http.Transport{MaxIdleConnsPerHost: 0} + + transport := newGeoHTTPTransport(NewGeoDNSPolicy("", true)) + if transport.MaxIdleConnsPerHost != geoHTTPDefaultMaxIdleConnsPerHost { + t.Fatalf("MaxIdleConnsPerHost = %d, want %d", transport.MaxIdleConnsPerHost, geoHTTPDefaultMaxIdleConnsPerHost) + } +} + func TestGeoHTTPTransportUsesProxyEnvironmentInFreshProcess(t *testing.T) { const helperEnv = "NEXTTRACE_GEO_PROXY_HELPER" if os.Getenv(helperEnv) == "1" { @@ -247,6 +258,57 @@ func TestNewSharedGeoHTTPClientReusesConnectionsAcrossClientWrappers(t *testing. } } +func TestNewSharedGeoHTTPClientReusesConnectionsAcrossConcurrentWrappers(t *testing.T) { + var newConnections atomic.Int32 + server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + _, _ = io.WriteString(w, "geo") + })) + server.Config.ConnState = func(_ net.Conn, state http.ConnState) { + if state == http.StateNew { + newConnections.Add(1) + } + } + server.Start() + defer server.Close() + + policy := NewGeoDNSPolicy("concurrent-reuse", true) + clients := make([]*http.Client, 4) + for i := range clients { + clients[i] = NewSharedGeoHTTPClientWithPolicy(2*time.Second, policy) + } + defer clients[0].CloseIdleConnections() + + for round := 0; round < 100; round++ { + var wg sync.WaitGroup + errCh := make(chan error, len(clients)) + for _, client := range clients { + wg.Go(func() { + resp, err := client.Get(server.URL) + if err != nil { + errCh <- err + return + } + _, readErr := io.Copy(io.Discard, resp.Body) + closeErr := resp.Body.Close() + if readErr != nil { + errCh <- readErr + } else if closeErr != nil { + errCh <- closeErr + } + }) + } + wg.Wait() + close(errCh) + for err := range errCh { + t.Fatalf("round %d: %v", round, err) + } + } + + if got := newConnections.Load(); got > 8 { + t.Fatalf("new connections = %d, want at most 8 for 400 concurrent requests", got) + } +} + func TestGeoHTTPTransportPoolEvictsOldestAndClosesIdleConnections(t *testing.T) { idle := make(chan struct{}, 1) closed := make(chan struct{}, 1)