Skip to content

Commit 19e1734

Browse files
committed
rate-limit routes calculation performed during topology broadcast updates
Fixes #105 rate-limit routes calculation done when a gossip topology update is recieved
1 parent 512bdb7 commit 19e1734

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

routes.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package mesh
22

33
import (
4+
"context"
45
"math"
56
"sync"
7+
8+
"golang.org/x/time/rate"
69
)
710

811
type unicastRoutes map[PeerName]PeerName
912
type broadcastRoutes map[PeerName][]PeerName
1013

14+
var limiter = rate.NewLimiter(2, 4)
15+
var pendingCalculate = false
16+
1117
// routes aggregates unicast and broadcast routes for our peer.
1218
type routes struct {
1319
sync.RWMutex
@@ -191,6 +197,22 @@ func (r *routes) run(recalculate <-chan *struct{}, wait <-chan chan struct{}, ac
191197
}
192198

193199
func (r *routes) calculate() {
200+
201+
// rate limit the number of calls to calculate(), also in case
202+
// if one or more calls were not allowed, then perform just one
203+
// more calculate() to accommodate latest updates
204+
if limiter.Allow() == false {
205+
r.Lock()
206+
if pendingCalculate {
207+
r.Unlock()
208+
return
209+
}
210+
pendingCalculate = true
211+
r.Unlock()
212+
limiter.Wait(context.TODO())
213+
pendingCalculate = false
214+
}
215+
194216
r.peers.RLock()
195217
r.ourself.RLock()
196218
var (

0 commit comments

Comments
 (0)