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

Fix various leaks #115

Merged
merged 2 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
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
13 changes: 12 additions & 1 deletion common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Fetch(uri, host, profile string, client *retryablehttp.Client) func() ([]by
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
Expand All @@ -59,6 +59,7 @@ func Fetch(uri, host, profile string, client *retryablehttp.Client) func() ([]by
if err != nil {
return nil, err
}
defer EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand Down Expand Up @@ -86,6 +87,7 @@ func Fetch(uri, host, profile string, client *retryablehttp.Client) func() ([]by
if err != nil {
return nil, fmt.Errorf("Retry DoRequest failed - " + err.Error())
}
defer EmptyAndCloseBody(resp)
if resp.StatusCode == http.StatusUnauthorized {
return nil, ErrInvalidCredential
}
Expand All @@ -102,6 +104,15 @@ func Fetch(uri, host, profile string, client *retryablehttp.Client) func() ([]by
}
}

// This is required to have a proper cleanup of the response body
// to have correctly working keep-alive connections
func EmptyAndCloseBody(resp *http.Response) {
if resp.Body != nil {
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
}

func BuildRequest(uri, host string) *retryablehttp.Request {
var user, password string

Expand Down
17 changes: 4 additions & 13 deletions exporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,13 @@ func NewExporter(ctx context.Context, target, uri, profile, model string, exclud
ExpectContinueTimeout: 1 * time.Second,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: config.GetConfig().SSLVerify,
Renegotiation: tls.RenegotiateOnceAsClient,
},
TLSHandshakeTimeout: 10 * time.Second,
}

defer tr.CloseIdleConnections()

retryClient := retryablehttp.NewClient()
retryClient.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy
retryClient.HTTPClient.Transport = tr
Expand Down Expand Up @@ -526,11 +529,7 @@ func (e *Exporter) collectMetrics(metrics chan<- prometheus.Metric) {
}

func (e *Exporter) scrape() {

var result uint8
state := uint8(1)
scrapes := len(e.pool.Tasks)
scrapeChan := make(chan uint8, scrapes)

// Concurrently call the endpoints to help prevent reaching the maxiumum number of 4 simultaneous sessions
e.pool.Run()
Expand Down Expand Up @@ -562,22 +561,14 @@ func (e *Exporter) scrape() {
}

if err != nil {
state = 0
log.Error("error exporting metrics", zap.Error(err), zap.String("url", task.URL), zap.Any("trace_id", e.ctx.Value("traceID")))
continue
}
scrapeChan <- 1
}

// Get scrape results from goroutine(s) and perform bitwise AND, any failures should
// result in a scrape failure
for i := 0; i < scrapes; i++ {
result = <-scrapeChan
state &= result
}

var upMetric = (*e.DeviceMetrics)["up"]
(*upMetric)["up"].WithLabelValues().Set(float64(state))

}

func (e *Exporter) GetContext() context.Context {
Expand Down
17 changes: 10 additions & 7 deletions exporter/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func getMemberUrls(url, host string, client *retryablehttp.Client) ([]string, er
if err != nil {
return urls, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusUnauthorized {
return urls, common.ErrInvalidCredential
Expand Down Expand Up @@ -77,7 +77,7 @@ func getSystemEndpoints(chassisUrls []string, host string, client *retryablehttp
if err != nil {
return sysEnd, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusUnauthorized {
return sysEnd, common.ErrInvalidCredential
Expand Down Expand Up @@ -218,7 +218,7 @@ func getSystemsMetadata(url, host string, client *retryablehttp.Client) (oem.Sys
if err != nil {
return sys, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
return sys, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
Expand Down Expand Up @@ -247,15 +247,16 @@ func getDIMMEndpoints(url, host string, client *retryablehttp.Client) (oem.Colle
if err != nil {
return dimms, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 1 && resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
time.Sleep(client.RetryWaitMin)
resp, err = common.DoRequest(client, req)
if err != nil {
return dimms, err
}
defer common.EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand Down Expand Up @@ -294,7 +295,7 @@ func getDriveEndpoint(url, host string, client *retryablehttp.Client) (oem.Gener
if err != nil {
return drive, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
Expand All @@ -303,6 +304,7 @@ func getDriveEndpoint(url, host string, client *retryablehttp.Client) (oem.Gener
if err != nil {
return drive, err
}
defer common.EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand Down Expand Up @@ -473,7 +475,7 @@ func getProcessorEndpoints(url, host string, client *retryablehttp.Client) (oem.
if err != nil {
return processors, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
Expand All @@ -482,6 +484,7 @@ func getProcessorEndpoints(url, host string, client *retryablehttp.Client) (oem.
if err != nil {
return processors, err
}
defer common.EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion exporter/moonshot/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func fetch(uri, device, metricType, host, profile string, client *retryablehttp.
if err != nil {
return nil, device, metricType, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
Expand All @@ -215,6 +215,7 @@ func fetch(uri, device, metricType, host, profile string, client *retryablehttp.
if err != nil {
return nil, device, metricType, err
}
defer common.EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand All @@ -239,6 +240,7 @@ func fetch(uri, device, metricType, host, profile string, client *retryablehttp.

time.Sleep(client.RetryWaitMin)
resp, err = common.DoRequest(client, req)
defer common.EmptyAndCloseBody(resp)
if err != nil {
return nil, device, metricType, fmt.Errorf("HTTP status %d", resp.StatusCode)
}
Expand Down
9 changes: 5 additions & 4 deletions plugins/nuova/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,13 @@ func checkRaidController(url, host string, client *retryablehttp.Client) (bool,
defer resp.Body.Close()
if !(resp.StatusCode >= http.StatusOK && resp.StatusCode < http.StatusMultipleChoices) {
if resp.StatusCode == http.StatusNotFound {
for retryCount < 1 && resp.StatusCode == http.StatusNotFound {
for retryCount < 3 && resp.StatusCode == http.StatusNotFound {
time.Sleep(client.RetryWaitMin)
resp, err = common.DoRequest(client, req)
if err != nil {
return false, nil
}
defer common.EmptyAndCloseBody(resp)
retryCount = retryCount + 1
}
if err != nil {
Expand Down Expand Up @@ -133,7 +134,7 @@ func IMCPost(uri, classId, cookie string, client *retryablehttp.Client) ([]byte,
if err != nil {
return nil, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand All @@ -151,7 +152,7 @@ func IMCLogin(uri, target string, client *retryablehttp.Client) (string, error)
if err != nil {
return aaaLogin.OutCookie, err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)

body, err := io.ReadAll(resp.Body)
if err != nil {
Expand All @@ -177,7 +178,7 @@ func IMCLogout(uri, cookie string, client *retryablehttp.Client) error {
if err != nil {
return err
}
defer resp.Body.Close()
defer common.EmptyAndCloseBody(resp)

return nil
}
Expand Down
Loading