Merge pull request #594 from noxiouz/add_rlocks_to_swarm_cluster

Add Rlocks to swarm cluster
This commit is contained in:
Andrea Luzzardi 2015-04-16 12:20:58 -07:00
commit 4db132e20e
1 changed files with 15 additions and 8 deletions

View File

@ -160,6 +160,9 @@ func (c *Cluster) newEntries(entries []*discovery.Entry) {
} }
func (c *Cluster) hasEngine(addr string) bool { func (c *Cluster) hasEngine(addr string) bool {
c.RLock()
defer c.RUnlock()
for _, engine := range c.engines { for _, engine := range c.engines {
if engine.Addr == addr { if engine.Addr == addr {
return true return true
@ -208,10 +211,15 @@ func (c *Cluster) RemoveImage(image *cluster.Image) ([]*dockerclient.ImageDelete
// Pull is exported // Pull is exported
func (c *Cluster) Pull(name string, callback func(what, status string)) { func (c *Cluster) Pull(name string, callback func(what, status string)) {
size := len(c.engines) var wg sync.WaitGroup
done := make(chan bool, size)
c.RLock()
for _, n := range c.engines { for _, n := range c.engines {
wg.Add(1)
go func(nn *cluster.Engine) { go func(nn *cluster.Engine) {
defer wg.Done()
if callback != nil { if callback != nil {
callback(nn.Name, "") callback(nn.Name, "")
} }
@ -223,12 +231,11 @@ func (c *Cluster) Pull(name string, callback func(what, status string)) {
callback(nn.Name, "downloaded") callback(nn.Name, "downloaded")
} }
} }
done <- true
}(n) }(n)
} }
for i := 0; i < size; i++ { c.RUnlock()
<-done
} wg.Wait()
} }
// Containers returns all the containers in the cluster. // Containers returns all the containers in the cluster.
@ -267,7 +274,7 @@ func (c *Cluster) listNodes() []*node.Node {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
out := []*node.Node{} out := make([]*node.Node, 0, len(c.engines))
for _, n := range c.engines { for _, n := range c.engines {
out = append(out, node.NewNode(n)) out = append(out, node.NewNode(n))
} }
@ -280,7 +287,7 @@ func (c *Cluster) listEngines() []*cluster.Engine {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()
out := []*cluster.Engine{} out := make([]*cluster.Engine, 0, len(c.engines))
for _, n := range c.engines { for _, n := range c.engines {
out = append(out, n) out = append(out, n)
} }