diff --git a/client.go b/client.go index 23eb318..a4b11c8 100644 --- a/client.go +++ b/client.go @@ -25,8 +25,8 @@ const ( // defaultRequestTimeout is the default per-request timeout (was 6s prior to v4.2.0). defaultRequestTimeout = 30 * time.Second - // defaultMaxConnsPerHost caps concurrent TCP connections per host. - defaultMaxConnsPerHost = 5 + // defaultMaxConnsPerHost caps concurrent TCP connections per host and sizes the idle pool; extra requests wait for a free connection. + defaultMaxConnsPerHost = 100 // defaultIdleTimeout sits below the typical 60s LB idle timeout with a 5s safety margin. defaultIdleTimeout = 55 * time.Second // defaultConnectTimeout caps TCP + TLS handshake duration. @@ -105,7 +105,7 @@ func WithTimeout(t time.Duration) ClientOption { } } -// WithMaxConnsPerHost caps concurrent TCP connections per host. Default: 5. +// WithMaxConnsPerHost caps concurrent TCP connections per host, which is also the number of idle connections kept for reuse. Default: 100. // Ignored when WithHTTPClient is set. func WithMaxConnsPerHost(n int) ClientOption { return func(c *Client) { @@ -209,6 +209,7 @@ func buildDefaultHTTPClient(requestTimeout time.Duration, maxConnsPerHost int, i transport := http.DefaultTransport.(*http.Transport).Clone() transport.MaxConnsPerHost = maxConnsPerHost transport.MaxIdleConnsPerHost = maxConnsPerHost + transport.MaxIdleConns = 0 // DefaultTransport's global 100 would otherwise cap the per-host idle pool transport.IdleConnTimeout = idleTimeout transport.DialContext = (&net.Dialer{ Timeout: connectTimeout, diff --git a/client_test.go b/client_test.go index 08cc864..48e2b45 100644 --- a/client_test.go +++ b/client_test.go @@ -212,8 +212,9 @@ func TestClientDefaultTransportConfig(t *testing.T) { tr, ok := httpClient.Transport.(*http.Transport) require.True(t, ok, "default transport must be *http.Transport, not nil") - assert.Equal(t, 5, tr.MaxConnsPerHost, "default MaxConnsPerHost = 5") - assert.Equal(t, 5, tr.MaxIdleConnsPerHost, "default MaxIdleConnsPerHost = 5") + assert.Equal(t, 100, tr.MaxConnsPerHost, "default MaxConnsPerHost = 100") + assert.Equal(t, 100, tr.MaxIdleConnsPerHost, "default MaxIdleConnsPerHost = 100") + assert.Equal(t, 0, tr.MaxIdleConns, "no global idle cap below the per-host one") assert.Equal(t, 55*time.Second, tr.IdleConnTimeout, "default IdleTimeout = 55s") assert.False(t, tr.DisableKeepAlives, "KeepAlive invariant") } @@ -257,7 +258,7 @@ func TestClientInfoLogOnConstruction(t *testing.T) { got := cap.infos[0] assert.Contains(t, got, "client.initialized") assert.Contains(t, got, "stream.sdk.name=getstream-go") - assert.Contains(t, got, "stream.client.max_conns_per_host=5") + assert.Contains(t, got, "stream.client.max_conns_per_host=100") assert.Contains(t, got, "stream.client.idle_timeout_seconds=55") assert.Contains(t, got, "stream.client.connect_timeout_seconds=10") assert.Contains(t, got, "stream.client.request_timeout_seconds=30")