Prevent race conditions when generating archives

This commit is contained in:
Dane Everitt
2020-07-16 19:56:53 -07:00
parent b2d34cf8e7
commit 7e1b7e7f36
3 changed files with 33 additions and 8 deletions

View File

@@ -3,12 +3,15 @@ package server
import (
"github.com/docker/docker/api/types"
"math"
"sync"
)
// Defines the current resource usage for a given server instance. If a server is offline you
// should obviously expect memory and CPU usage to be 0. However, disk will always be returned
// since that is not dependent on the server being running to collect that data.
type ResourceUsage struct {
sync.RWMutex
// The total amount of memory, in bytes, that this server instance is consuming. This is
// calculated slightly differently than just using the raw Memory field that the stats
// return from the container, so please check the code setting this value for how that
@@ -31,6 +34,18 @@ type ResourceUsage struct {
} `json:"network"`
}
// Resets the usages values to zero, used when a server is stopped to ensure we don't hold
// onto any values incorrectly.
func (ru *ResourceUsage) Empty() {
ru.Lock()
defer ru.Unlock()
ru.Memory = 0
ru.CpuAbsolute = 0
ru.Network.TxBytes = 0
ru.Network.RxBytes = 0
}
// The "docker stats" CLI call does not return the same value as the types.MemoryStats.Usage
// value which can be rather confusing to people trying to compare panel usage to
// their stats output.