Skip to content

Commit

Permalink
Merge pull request #110 from weaveworks/coalesce-ensure-recalculated
Browse files Browse the repository at this point in the history
Allow multiple calls to ensureRecalculated() to wait on the same calculation
  • Loading branch information
bboreham committed Oct 11, 2019
2 parents ea482fe + 4e69d97 commit 5a78851
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type routes struct {
broadcast broadcastRoutes
broadcastAll broadcastRoutes // [1]
recalc chan<- *struct{}
wait chan<- chan struct{}
wait chan chan struct{}
action chan<- func()
// [1] based on *all* connections, not just established &
// symmetric ones
Expand Down Expand Up @@ -167,7 +167,13 @@ func (r *routes) recalculate() {

// EnsureRecalculated waits for any preceding Recalculate requests to finish.
func (r *routes) ensureRecalculated() {
done := make(chan struct{})
var done chan struct{}
// If another call is already waiting, wait on the same chan, otherwise make a new one
select {
case done = <-r.wait:
default:
done = make(chan struct{})
}
r.wait <- done
<-done
}
Expand Down

0 comments on commit 5a78851

Please sign in to comment.