Skip to content

Commit

Permalink
Merge pull request #34 from bobrik/fix-concurrent-map-writes
Browse files Browse the repository at this point in the history
Fix concurrent map write on balancer update
  • Loading branch information
bobrik committed Apr 4, 2016
2 parents 347e6a4 + 5194f2f commit 36953d6
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions explorer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,9 @@ func (e *Explorer) discover() (*Discovery, error) {
func (e *Explorer) updateBalancers(discovery *Discovery) {
state := e.getState()

wg := sync.WaitGroup{}

now := time.Now()

updates := []balancer.Balancer{}
for _, b := range discovery.Balancers {
bs := b.String()
if reflect.DeepEqual(e.updated[bs].apps, discovery.Apps) {
Expand All @@ -116,8 +115,13 @@ func (e *Explorer) updateBalancers(discovery *Discovery) {
}
}

wg.Add(1)
updates = append(updates, b)
}

wg := sync.WaitGroup{}
wg.Add(len(updates))

for _, b := range updates {
go func(b balancer.Balancer) {
defer wg.Done()

Expand All @@ -127,10 +131,12 @@ func (e *Explorer) updateBalancers(discovery *Discovery) {
return
}

e.mutex.Lock()
e.updated[b.String()] = update{
time: now,
apps: discovery.Apps,
}
e.mutex.Unlock()
}(b)
}

Expand Down

0 comments on commit 36953d6

Please sign in to comment.