mirror of
https://github.com/fankes/beszel.git
synced 2025-10-20 02:09:28 +08:00
make sure deletion of container stats is thread safe
This commit is contained in:
@@ -175,7 +175,7 @@ func getDockerStats() ([]*ContainerStats, error) {
|
|||||||
// note: can't use Created field because it's not updated on restart
|
// note: can't use Created field because it's not updated on restart
|
||||||
if strings.HasSuffix(ctr.Status, "seconds") {
|
if strings.HasSuffix(ctr.Status, "seconds") {
|
||||||
// if so, remove old container data
|
// if so, remove old container data
|
||||||
delete(containerStatsMap, ctr.IdShort)
|
deleteContainerStatsSync(ctr.IdShort)
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
@@ -183,7 +183,7 @@ func getDockerStats() ([]*ContainerStats, error) {
|
|||||||
cstats, err := getContainerStats(ctr)
|
cstats, err := getContainerStats(ctr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// delete container from map and retry once
|
// delete container from map and retry once
|
||||||
delete(containerStatsMap, ctr.IdShort)
|
deleteContainerStatsSync(ctr.IdShort)
|
||||||
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)
|
||||||
@@ -277,6 +277,13 @@ func getContainerStats(ctr *Container) (*ContainerStats, error) {
|
|||||||
return cStats, nil
|
return cStats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// delete container stats from map using mutex
|
||||||
|
func deleteContainerStatsSync(id string) {
|
||||||
|
containerStatsMutex.Lock()
|
||||||
|
defer containerStatsMutex.Unlock()
|
||||||
|
delete(containerStatsMap, id)
|
||||||
|
}
|
||||||
|
|
||||||
func gatherStats() *SystemData {
|
func gatherStats() *SystemData {
|
||||||
systemInfo, systemStats := getSystemStats()
|
systemInfo, systemStats := getSystemStats()
|
||||||
stats := &SystemData{
|
stats := &SystemData{
|
||||||
|
Reference in New Issue
Block a user