Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use Content Length hint to pre-allocate buffer when reading body from sysprobe #30689

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 27 additions & 8 deletions pkg/process/net/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func (r *RemoteSysProbeUtil) GetProcStats(pids []int32) (*model.ProcStatsWithPer
return nil, fmt.Errorf("proc_stats request failed: Probe Path %s, url: %s, status code: %d", r.path, procStatsURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func (r *RemoteSysProbeUtil) GetConnections(clientID string) (*model.Connections
return nil, fmt.Errorf("conn request failed: Probe Path %s, url: %s, status code: %d", r.path, connectionsURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,7 +188,7 @@ func (r *RemoteSysProbeUtil) GetNetworkID() (string, error) {
return "", fmt.Errorf("network_id request failed: url: %s, status code: %d", networkIDURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return "", fmt.Errorf("failed to read response body: %w", err)
}
Expand All @@ -211,7 +211,7 @@ func (r *RemoteSysProbeUtil) GetPing(clientID string, host string, count int, in
defer resp.Body.Close()

if resp.StatusCode == http.StatusBadRequest {
body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, fmt.Errorf("ping request failed: Probe Path %s, url: %s, status code: %d", r.path, pingURL, resp.StatusCode)
}
Expand All @@ -220,7 +220,7 @@ func (r *RemoteSysProbeUtil) GetPing(clientID string, host string, count int, in
return nil, fmt.Errorf("ping request failed: Probe Path %s, url: %s, status code: %d", r.path, pingURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -248,7 +248,7 @@ func (r *RemoteSysProbeUtil) GetTraceroute(clientID string, host string, port ui
defer resp.Body.Close()

if resp.StatusCode == http.StatusBadRequest {
body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, fmt.Errorf("traceroute request failed: Probe Path %s, url: %s, status code: %d", r.path, tracerouteURL, resp.StatusCode)
}
Expand All @@ -257,7 +257,7 @@ func (r *RemoteSysProbeUtil) GetTraceroute(clientID string, host string, port ui
return nil, fmt.Errorf("traceroute request failed: Probe Path %s, url: %s, status code: %d", r.path, tracerouteURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, err
}
Expand All @@ -282,7 +282,7 @@ func (r *RemoteSysProbeUtil) GetStats() (map[string]interface{}, error) {
return nil, fmt.Errorf("conn request failed: Path %s, url: %s, status code: %d", r.path, statsURL, resp.StatusCode)
}

body, err := io.ReadAll(resp.Body)
body, err := readAllResponseBody(resp)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -487,3 +487,22 @@ func (r *RemoteSysProbeUtil) init() error {
}
return nil
}

func readAllResponseBody(resp *http.Response) ([]byte, error) {
// if we are not able to determine the content length
// we read the whole body without pre-allocation
if resp.ContentLength <= 0 {
return io.ReadAll(resp.Body)
}

// if we know the content length we pre-allocate the buffer
var buf bytes.Buffer
buf.Grow(int(resp.ContentLength))

_, err := buf.ReadFrom(resp.Body)
if err != nil {
return nil, err
}

return buf.Bytes(), nil
}
Loading