706
707
// consume updates ready based on the current time and number of new tokens consumed.
708
>
func (ready simpleLimiter) consume(p simpleLimiterParams, now int64, tokens int64) simpleLimiter {
matcher_data.go
709
>
// This is a slight variation of the normal GCRA: instead of tracking the end of the
710
>
// allowed interval (the theoretical arrival time), ready tracks the beginning of it, and
711
>
// the end is ready + burst. To find the next ready time:
712
>
// - Add ready+burst to find the next theoretical arrival time.
713
>
// - If that's in the past, clip it at the current time.
714
>
// - Subtract burst to turn it back into a ready time.
715
>
// - Finally add the tokens we used.
716
>
//
717
>
// For intuition, consider that if if now is > ready by only a tiny amount, i.e. we're
718
>
// bursting, then the max takes ready+burst and we push up the ready time by the full
719
>
// interval. We can do this burst/interval times before it catches up and we're no longer
720
>
// ready.
721
>
//
722
>
// Alternatively, if now is > ready by more than burst, then we end up subtracting the full
723
>
// burst from now and adding one interval.
724
>
if p.never() {
725
return simpleLimiterNever
726
}
727
>
clippedReady := max(now, int64(ready)+p.burst.Nanoseconds()) - p.burst.Nanoseconds()
matcher_data.go
728
>
return simpleLimiter(clippedReady + tokens*p.interval.Nanoseconds())
729
}
730