Skip to content

Commit 274e000

Browse files
committed
⚡ when high concurrency occurs, the connection is full and the connection is rejected.
Signed-off-by: cuisongliu <[email protected]>
1 parent 7d3be80 commit 274e000

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

api/client.go

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ func (cfg *Config) validate() error {
7575

7676
// Client is the interface for an API client.
7777
type Client interface {
78+
CloseIdleConnections()
7879
URL(ep string, args map[string]string) *url.URL
7980
Do(context.Context, *http.Request) (*http.Response, []byte, error)
8081
}
@@ -118,10 +119,15 @@ func (c *httpClient) URL(ep string, args map[string]string) *url.URL {
118119
return &u
119120
}
120121

122+
func (c *httpClient) CloseIdleConnections() {
123+
c.client.CloseIdleConnections()
124+
}
125+
121126
func (c *httpClient) Do(ctx context.Context, req *http.Request) (*http.Response, []byte, error) {
122127
if ctx != nil {
123128
req = req.WithContext(ctx)
124129
}
130+
defer c.client.CloseIdleConnections()
125131
resp, err := c.client.Do(req)
126132
defer func() {
127133
if resp != nil {

api/prometheus/v1/api.go

+4
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ type Warnings []string
13421342
// apiClient wraps a regular client and processes successful API responses.
13431343
// Successful also includes responses that errored at the API level.
13441344
type apiClient interface {
1345+
CloseIdleConnections()
13451346
URL(ep string, args map[string]string) *url.URL
13461347
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
13471348
DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error)
@@ -1373,6 +1374,9 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
13731374
}
13741375
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
13751376
}
1377+
func (h *apiClientImpl) CloseIdleConnections() {
1378+
h.client.CloseIdleConnections()
1379+
}
13761380

13771381
func (h *apiClientImpl) URL(ep string, args map[string]string) *url.URL {
13781382
return h.client.URL(ep, args)

api/prometheus/v1/api_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
6262
return u
6363
}
6464

65+
func (c *apiTestClient) CloseIdleConnections() {
66+
}
6567
func (c *apiTestClient) Do(_ context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
6668
test := c.curTest
6769

@@ -1263,6 +1265,9 @@ type apiClientTest struct {
12631265
expectedWarnings Warnings
12641266
}
12651267

1268+
func (c *testClient) CloseIdleConnections() {
1269+
}
1270+
12661271
func (c *testClient) URL(ep string, args map[string]string) *url.URL {
12671272
return nil
12681273
}
@@ -1734,6 +1739,9 @@ type httpTestClient struct {
17341739
client http.Client
17351740
}
17361741

1742+
func (c *httpTestClient) CloseIdleConnections() {
1743+
c.client.CloseIdleConnections()
1744+
}
17371745
func (c *httpTestClient) URL(ep string, args map[string]string) *url.URL {
17381746
return nil
17391747
}

0 commit comments

Comments
 (0)