mirror of
https://github.com/ribbybibby/ssl_exporter.git
synced 2026-02-14 17:49:58 +00:00
Fix connection leak (#31)
Connections were being left around after requests and in some cases this could result in file descriptor errors when open files built up. Closing the http response body and the tcp connection, as well as disabling http keep alives seems to resolve this.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"crypto/x509"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -96,8 +97,9 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||
return http.ErrUseLastResponse
|
||||
},
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: e.tlsConfig,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
TLSClientConfig: e.tlsConfig,
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
DisableKeepAlives: true,
|
||||
},
|
||||
Timeout: e.timeout,
|
||||
}
|
||||
@@ -111,6 +113,13 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||
)
|
||||
return
|
||||
}
|
||||
defer func() {
|
||||
_, err := io.Copy(ioutil.Discard, resp.Body)
|
||||
if err != nil {
|
||||
log.Errorln(err)
|
||||
}
|
||||
resp.Body.Close()
|
||||
}()
|
||||
|
||||
// Check if the response from the target is encrypted
|
||||
if resp.TLS == nil {
|
||||
@@ -136,6 +145,7 @@ func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
|
||||
)
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
state = conn.ConnectionState()
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user