From ac1acdfc71b7c5a779fb79eb56a2c5a749a7488b Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Thu, 9 May 2024 21:01:10 +0800 Subject: [PATCH] :zap: when high concurrency occurs, the connection is full and the connection is rejected. Signed-off-by: cuisongliu --- api/client.go | 12 ++++++++++++ api/prometheus/v1/api_test.go | 3 +++ 2 files changed, 15 insertions(+) diff --git a/api/client.go b/api/client.go index 72a01309c..e8e0e97f2 100644 --- a/api/client.go +++ b/api/client.go @@ -79,6 +79,10 @@ type Client interface { Do(context.Context, *http.Request) (*http.Response, []byte, error) } +type closeIdler interface { + CloseIdleConnections() +} + // NewClient returns a new Client. // // It is safe to use the returned Client from multiple goroutines. @@ -99,6 +103,10 @@ func NewClient(cfg Config) (Client, error) { }, nil } +func ClientCloseIdler(cl Client) { + cl.(closeIdler).CloseIdleConnections() +} + type httpClient struct { endpoint *url.URL client http.Client @@ -118,6 +126,10 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL { return &u } +func (c *httpClient) CloseIdleConnections() { + c.client.CloseIdleConnections() +} + func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) { if ctx != nil { req = req.WithContext(ctx) diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index e87e48311..57905ade7 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -1263,6 +1263,9 @@ type apiClientTest struct { expectedWarnings Warnings } +func (c *testClient) CloseIdleConnections() { +} + func (c *testClient) URL(ep string, args map[string]string) *url.URL { return nil }