mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
add lock to container map in dockersource
This commit is contained in:
parent
c84b52ce40
commit
2e51406d00
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/bcicen/ctop/metrics"
|
"github.com/bcicen/ctop/metrics"
|
||||||
"github.com/fsouza/go-dockerclient"
|
"github.com/fsouza/go-dockerclient"
|
||||||
@ -17,6 +18,7 @@ type DockerContainerSource struct {
|
|||||||
client *docker.Client
|
client *docker.Client
|
||||||
containers map[string]*Container
|
containers map[string]*Container
|
||||||
needsRefresh chan string // container IDs requiring refresh
|
needsRefresh chan string // container IDs requiring refresh
|
||||||
|
lock sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDockerContainerSource() *DockerContainerSource {
|
func NewDockerContainerSource() *DockerContainerSource {
|
||||||
@ -29,6 +31,7 @@ func NewDockerContainerSource() *DockerContainerSource {
|
|||||||
client: client,
|
client: client,
|
||||||
containers: make(map[string]*Container),
|
containers: make(map[string]*Container),
|
||||||
needsRefresh: make(chan string, 60),
|
needsRefresh: make(chan string, 60),
|
||||||
|
lock: sync.RWMutex{},
|
||||||
}
|
}
|
||||||
go cm.Loop()
|
go cm.Loop()
|
||||||
cm.refreshAll()
|
cm.refreshAll()
|
||||||
@ -112,28 +115,36 @@ func (cm *DockerContainerSource) MustGet(id string) *Container {
|
|||||||
collector := metrics.NewDocker(cm.client, id)
|
collector := metrics.NewDocker(cm.client, id)
|
||||||
// create container
|
// create container
|
||||||
c = NewContainer(id, collector)
|
c = NewContainer(id, collector)
|
||||||
|
cm.lock.Lock()
|
||||||
cm.containers[id] = c
|
cm.containers[id] = c
|
||||||
|
cm.lock.Unlock()
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a single container, by ID
|
// Get a single container, by ID
|
||||||
func (cm *DockerContainerSource) Get(id string) (*Container, bool) {
|
func (cm *DockerContainerSource) Get(id string) (*Container, bool) {
|
||||||
|
cm.lock.Lock()
|
||||||
c, ok := cm.containers[id]
|
c, ok := cm.containers[id]
|
||||||
|
cm.lock.Unlock()
|
||||||
return c, ok
|
return c, ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove containers by ID
|
// Remove containers by ID
|
||||||
func (cm *DockerContainerSource) delByID(id string) {
|
func (cm *DockerContainerSource) delByID(id string) {
|
||||||
|
cm.lock.Lock()
|
||||||
delete(cm.containers, id)
|
delete(cm.containers, id)
|
||||||
|
cm.lock.Unlock()
|
||||||
log.Infof("removed dead container: %s", id)
|
log.Infof("removed dead container: %s", id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return array of all containers, sorted by field
|
// Return array of all containers, sorted by field
|
||||||
func (cm *DockerContainerSource) All() (containers Containers) {
|
func (cm *DockerContainerSource) All() (containers Containers) {
|
||||||
|
cm.lock.Lock()
|
||||||
for _, c := range cm.containers {
|
for _, c := range cm.containers {
|
||||||
containers = append(containers, c)
|
containers = append(containers, c)
|
||||||
}
|
}
|
||||||
|
cm.lock.Unlock()
|
||||||
sort.Sort(containers)
|
sort.Sort(containers)
|
||||||
containers.Filter()
|
containers.Filter()
|
||||||
return containers
|
return containers
|
||||||
|
Loading…
Reference in New Issue
Block a user