File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -504,6 +504,8 @@ func (thlr *Throttler) initiateThrottlerChecks() {
504504// throttle sees if throttling needs take place, and if so, continuously sleeps (blocks)
505505// until throttling reasons are gone
506506func (thlr * Throttler ) throttle (onThrottled func ()) {
507+ timer := time .NewTimer (250 * time .Millisecond )
508+ defer timer .Stop ()
507509 for {
508510 // IsThrottled() is non-blocking; the throttling decision making takes place asynchronously.
509511 // Therefore calling IsThrottled() is cheap
@@ -513,10 +515,11 @@ func (thlr *Throttler) throttle(onThrottled func()) {
513515 if onThrottled != nil {
514516 onThrottled ()
515517 }
518+ timer .Reset (250 * time .Millisecond )
516519 select {
517520 case <- thlr .migrationContext .GetContext ().Done ():
518521 return
519- case <- time . After ( 250 * time . Millisecond ) :
522+ case <- timer . C :
520523 }
521524 }
522525}
Original file line number Diff line number Diff line change @@ -91,13 +91,20 @@ func TestThrottleCallsOnThrottledCallback(t *testing.T) {
9191 thlr .migrationContext .SetThrottled (true , "test" , base .NoThrottleReasonHint )
9292
9393 var callCount atomic.Int32
94+ done := make (chan struct {})
9495 go func () {
9596 thlr .throttle (func () { callCount .Add (1 ) })
97+ close (done )
9698 }()
9799
98100 // wait long enough for at least two callback invocations
99101 time .Sleep (700 * time .Millisecond )
100102 assert .GreaterOrEqual (t , callCount .Load (), int32 (2 ))
101103
102104 thlr .migrationContext .CancelContext ()
105+ select {
106+ case <- done :
107+ case <- time .After (1 * time .Second ):
108+ t .Fatal ("throttle() did not return after context cancellation" )
109+ }
103110}
You can’t perform that action at this time.
0 commit comments