Skip to content

Commit

Permalink
perf: Rest client allocs improvements (#2381)
Browse files Browse the repository at this point in the history
* perf: Rest client allocs improvements
  • Loading branch information
rahulguptajss authored Sep 27, 2023
1 parent be4b7f9 commit 6fb987d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
31 changes: 12 additions & 19 deletions cmd/tools/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) {

doInvoke := func() ([]byte, error) {
var (
response *http.Response
body []byte
err error
response *http.Response
innerBody []byte
innerErr error
)

if c.request.Body != nil {
Expand All @@ -174,23 +174,23 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) {
restReq := c.request.URL.String()

// send request to server
if response, err = c.client.Do(c.request); err != nil {
return nil, fmt.Errorf("connection error %w", err)
if response, innerErr = c.client.Do(c.request); innerErr != nil {
return nil, fmt.Errorf("connection error %w", innerErr)
}
//goland:noinspection GoUnhandledErrorResult
defer response.Body.Close()
innerBody, innerErr = io.ReadAll(response.Body)
if innerErr != nil {
return nil, errs.Rest(response.StatusCode, innerErr.Error(), 0, "")
}

if response.StatusCode != http.StatusOK {
body2, err2 := io.ReadAll(response.Body)
if err2 != nil {
return nil, errs.Rest(response.StatusCode, err2.Error(), 0, "")
}

if response.StatusCode == http.StatusUnauthorized {
return nil, errs.New(errs.ErrAuthFailed, response.Status)
}

result := gjson.GetBytes(body2, "error")
result := gjson.GetBytes(innerBody, "error")

if response.StatusCode == http.StatusForbidden {
message := result.Get(Message).String()
Expand All @@ -206,16 +206,9 @@ func (c *Client) invokeWithAuthRetry() ([]byte, error) {
return nil, errs.Rest(response.StatusCode, "", 0, "")
}

// read response body
if body, err = io.ReadAll(response.Body); err != nil {
return nil, err
}
defer c.printRequestAndResponse(restReq, body)
defer c.printRequestAndResponse(restReq, innerBody)

if err != nil {
return nil, err
}
return body, nil
return innerBody, nil
}

body, err = doInvoke()
Expand Down
2 changes: 1 addition & 1 deletion pkg/matrix/matrix.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ func (m *Matrix) Clone(with With) *Matrix {
clone.globalLabels = m.globalLabels
clone.exportOptions = m.exportOptions
clone.exportable = m.exportable
clone.displayMetrics = make(map[string]string)

if with.Instances {
clone.instances = make(map[string]*Instance, len(m.GetInstances()))
Expand All @@ -95,6 +94,7 @@ func (m *Matrix) Clone(with With) *Matrix {

if with.Metrics {
clone.metrics = make(map[string]*Metric, len(m.GetMetrics()))
clone.displayMetrics = make(map[string]string)
for key, metric := range m.GetMetrics() {
c := metric.Clone(with.Data)
clone.metrics[key] = c
Expand Down

0 comments on commit 6fb987d

Please sign in to comment.