mirror of
https://github.com/fankes/beszel.git
synced 2025-10-19 01:39:34 +08:00
close idle connections on timeout
This commit is contained in:
@@ -153,6 +153,7 @@ func getSystemStats() (*SystemInfo, *SystemStats) {
|
|||||||
func getDockerStats() ([]*ContainerStats, error) {
|
func getDockerStats() ([]*ContainerStats, error) {
|
||||||
resp, err := dockerClient.Get("http://localhost/containers/json")
|
resp, err := dockerClient.Get("http://localhost/containers/json")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
closeIdleConnections(err)
|
||||||
return []*ContainerStats{}, err
|
return []*ContainerStats{}, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
@@ -184,8 +185,15 @@ func getDockerStats() ([]*ContainerStats, error) {
|
|||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
cstats, err := getContainerStats(ctr)
|
cstats, err := getContainerStats(ctr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// delete container from map and retry once
|
// Check if the error is a network timeout
|
||||||
deleteContainerStatsSync(ctr.IdShort)
|
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||||
|
// Close idle connections to prevent reuse of stale connections
|
||||||
|
closeIdleConnections(err)
|
||||||
|
} else {
|
||||||
|
// otherwise delete container from map
|
||||||
|
deleteContainerStatsSync(ctr.IdShort)
|
||||||
|
}
|
||||||
|
// retry once
|
||||||
cstats, err = getContainerStats(ctr)
|
cstats, err = getContainerStats(ctr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error getting container stats: %+v\n", err)
|
log.Printf("Error getting container stats: %+v\n", err)
|
||||||
@@ -442,7 +450,7 @@ func newDockerClient() *http.Client {
|
|||||||
ForceAttemptHTTP2: false,
|
ForceAttemptHTTP2: false,
|
||||||
IdleConnTimeout: 90 * time.Second,
|
IdleConnTimeout: 90 * time.Second,
|
||||||
DisableCompression: true,
|
DisableCompression: true,
|
||||||
MaxIdleConnsPerHost: 50,
|
MaxIdleConnsPerHost: 20,
|
||||||
DisableKeepAlives: false,
|
DisableKeepAlives: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -465,3 +473,8 @@ func newDockerClient() *http.Client {
|
|||||||
Transport: transport,
|
Transport: transport,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func closeIdleConnections(err error) {
|
||||||
|
log.Printf("Closing idle connections. Error: %+v\n", err)
|
||||||
|
dockerClient.Transport.(*http.Transport).CloseIdleConnections()
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user