Skip to content

Commit 667577f

Browse files
committed
Remember to close HTTP body from response
Flagged via community in: #1836 Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
1 parent 8d5dcdf commit 667577f

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

gateway/metrics/exporter.go

+28-14
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri
8686

8787
namespaces, err := e.getNamespaces(endpointURL)
8888
if err != nil {
89-
log.Println(err)
89+
log.Printf("Error listing namespaces: %s", err)
9090
}
9191

9292
services := []types.FunctionStatus{}
@@ -95,15 +95,15 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri
9595
if len(namespaces) == 0 {
9696
services, err = e.getFunctions(endpointURL, e.FunctionNamespace)
9797
if err != nil {
98-
log.Println(err)
98+
log.Printf("Error getting functions from: %s, error: %s", e.FunctionNamespace, err)
9999
continue
100100
}
101101
e.services = services
102102
} else {
103103
for _, namespace := range namespaces {
104104
nsServices, err := e.getFunctions(endpointURL, namespace)
105105
if err != nil {
106-
log.Println(err)
106+
log.Printf("Error getting functions from: %s, error: %s", e.FunctionNamespace, err)
107107
continue
108108
}
109109
services = append(services, nsServices...)
@@ -112,7 +112,6 @@ func (e *Exporter) StartServiceWatcher(endpointURL url.URL, metricsOptions Metri
112112

113113
e.services = services
114114

115-
break
116115
case <-quit:
117116
return
118117
}
@@ -159,14 +158,22 @@ func (e *Exporter) getFunctions(endpointURL url.URL, namespace string) ([]types.
159158
return services, err
160159
}
161160

162-
bytesOut, readErr := io.ReadAll(res.Body)
163-
if readErr != nil {
164-
return services, readErr
161+
var body []byte
162+
if res.Body != nil {
163+
defer res.Body.Close()
164+
165+
if b, err := io.ReadAll(res.Body); err != nil {
166+
return services, err
167+
} else {
168+
body = b
169+
}
170+
} else {
171+
return services, fmt.Errorf("no response body from /system/functions")
165172
}
166173

167-
if err := json.Unmarshal(bytesOut, &services); err != nil {
174+
if err := json.Unmarshal(body, &services); err != nil {
168175
return services, fmt.Errorf("error unmarshalling response: %s, error: %s",
169-
string(bytesOut), err)
176+
string(body), err)
170177
}
171178

172179
return services, nil
@@ -193,13 +200,20 @@ func (e *Exporter) getNamespaces(endpointURL url.URL) ([]string, error) {
193200
return namespaces, nil
194201
}
195202

196-
bytesOut, readErr := io.ReadAll(res.Body)
197-
if readErr != nil {
198-
return namespaces, readErr
203+
var body []byte
204+
if res.Body != nil {
205+
defer res.Body.Close()
206+
207+
if b, err := io.ReadAll(res.Body); err != nil {
208+
return namespaces, err
209+
} else {
210+
body = b
211+
}
199212
}
200213

201-
if err := json.Unmarshal(bytesOut, &namespaces); err != nil {
202-
return namespaces, fmt.Errorf("error unmarshalling response: %s, error: %s", string(bytesOut), err)
214+
if err := json.Unmarshal(body, &namespaces); err != nil {
215+
return namespaces, fmt.Errorf("error unmarshalling response: %s, error: %s", string(body), err)
203216
}
217+
204218
return namespaces, nil
205219
}

0 commit comments

Comments
 (0)