Skip to content

Commit

Permalink
rate-limit routes calculation performed during topology broadcast upd…
Browse files Browse the repository at this point in the history
…ates

Fixes #105 rate-limit routes calculation done when a gossip topology update is recieved
  • Loading branch information
murali-reddy committed May 20, 2019
1 parent 512bdb7 commit 19e1734
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions routes.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package mesh

import (
"context"
"math"
"sync"

"golang.org/x/time/rate"
)

type unicastRoutes map[PeerName]PeerName
type broadcastRoutes map[PeerName][]PeerName

var limiter = rate.NewLimiter(2, 4)
var pendingCalculate = false

// routes aggregates unicast and broadcast routes for our peer.
type routes struct {
sync.RWMutex
Expand Down Expand Up @@ -191,6 +197,22 @@ func (r *routes) run(recalculate <-chan *struct{}, wait <-chan chan struct{}, ac
}

func (r *routes) calculate() {

// rate limit the number of calls to calculate(), also in case
// if one or more calls were not allowed, then perform just one
// more calculate() to accommodate latest updates
if limiter.Allow() == false {
r.Lock()
if pendingCalculate {
r.Unlock()
return
}
pendingCalculate = true
r.Unlock()
limiter.Wait(context.TODO())
pendingCalculate = false
}

r.peers.RLock()
r.ourself.RLock()
var (
Expand Down

0 comments on commit 19e1734

Please sign in to comment.