Skip to content

Commit

Permalink
Allow multiple calls to ensureRecalculated() to wait on the same calc…
Browse files Browse the repository at this point in the history
…ulation

If another call is already waiting, wait on the same chan.

This is an improvement on the previous behaviour that only allowed one
ensureRecalculated() to proceed per calculate(), in the presence of
many recalculate() requests.

It is now possible for a later call to overtake the first.  Previously
it was possible for a later call to overtake an earlier call except
for the first.
  • Loading branch information
bboreham committed Oct 8, 2019
1 parent b7aea39 commit 4e69d97
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 4e69d97

Please sign in to comment.