Skip to content

Commit

Permalink
⚡ when high concurrency occurs, the connection is full and the connec…
Browse files Browse the repository at this point in the history
…tion is rejected.

Signed-off-by: cuisongliu <cuisongliu@qq.com>
  • Loading branch information
cuisongliu committed May 16, 2024
1 parent 7d3be80 commit 9f1e4e5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func (cfg *Config) validate() error {
type Client interface {
URL(ep string, args map[string]string) *url.URL
Do(context.Context, *http.Request) (*http.Response, []byte, error)
CloseIdleConnections()
}

// NewClient returns a new Client.
Expand Down Expand Up @@ -118,6 +119,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)
Expand Down
5 changes: 5 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1342,6 +1342,7 @@ type Warnings []string
// apiClient wraps a regular client and processes successful API responses.
// Successful also includes responses that errored at the API level.
type apiClient interface {
CloseIdleConnections()
URL(ep string, args map[string]string) *url.URL
Do(context.Context, *http.Request) (*http.Response, []byte, Warnings, error)
DoGetFallback(ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, Warnings, error)
Expand Down Expand Up @@ -1374,6 +1375,10 @@ func errorTypeAndMsgFor(resp *http.Response) (ErrorType, string) {
return ErrBadResponse, fmt.Sprintf("bad response code %d", resp.StatusCode)
}

func (h *apiClientImpl) CloseIdleConnections() {
h.client.CloseIdleConnections()
}

func (h *apiClientImpl) URL(ep string, args map[string]string) *url.URL {
return h.client.URL(ep, args)
}
Expand Down
10 changes: 10 additions & 0 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ func (c *apiTestClient) URL(ep string, args map[string]string) *url.URL {
return u
}

func (c *apiTestClient) CloseIdleConnections() {
}

func (c *apiTestClient) Do(_ context.Context, req *http.Request) (*http.Response, []byte, Warnings, error) {
test := c.curTest

Expand Down Expand Up @@ -1263,6 +1266,9 @@ type apiClientTest struct {
expectedWarnings Warnings
}

func (c *testClient) CloseIdleConnections() {
}

func (c *testClient) URL(ep string, args map[string]string) *url.URL {
return nil
}
Expand Down Expand Up @@ -1734,6 +1740,10 @@ type httpTestClient struct {
client http.Client
}

func (c *httpTestClient) CloseIdleConnections() {
c.client.CloseIdleConnections()
}

func (c *httpTestClient) URL(ep string, args map[string]string) *url.URL {
return nil
}
Expand Down

0 comments on commit 9f1e4e5

Please sign in to comment.