Skip to content

Commit

Permalink
Fix the throttle oversupply counting (#124)
Browse files Browse the repository at this point in the history
The previous implementation had false positives for oversupply when the
limit was increased at runtime due to the channel not being drained than
the for loop could put 2N tokens in N pigeonholes.

The new implementation uses a timeout for each episode of the throttle
that will terminate the for loop early if the tokens can't all be sent
down the channel. I considered using the next tick of the main ticker as
the timeout, but this would have caused the next episode of the throttle
to be skipped without more complexity being added.
  • Loading branch information
mhutchinson authored Apr 24, 2024
1 parent 88868f8 commit 3f61737
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions hammer/hammer.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,16 @@ func (t *Throttle) Run(ctx context.Context) {
return
case <-ticker.C:
tokenCount := t.opsPerSecond
sessionOversupply := 0
timeout := time.After(1 * time.Second)
Loop:
for i := 0; i < tokenCount; i++ {
select {
case t.tokenChan <- true:
default:
sessionOversupply += 1
case <-timeout:
t.oversupply = tokenCount - i
break Loop
}
}
t.oversupply = sessionOversupply
}
}
}
Expand Down

0 comments on commit 3f61737

Please sign in to comment.