From 407d84394397f0aa21ed7b05ab611afdb2e3ba75 Mon Sep 17 00:00:00 2001 From: tsosunchia <59512455+tsosunchia@users.noreply.github.com> Date: Fri, 4 Sep 2026 18:03:44 +0800 Subject: [PATCH] =?UTF-8?q?perf(geo):=20=E6=89=A9=E5=B1=95=E5=85=B1?= =?UTF-8?q?=E4=BA=AB=E8=BF=9E=E6=8E=A5=E6=B1=A0=E7=A9=BA=E9=97=B2=E5=AE=B9?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- util/http_client_geo.go | 8 ++++- util/http_client_geo_test.go | 62 ++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) 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)