'Rate Limiting Cancellation token restore

I am trying to understand rate limiting and having a hard time understanding this logic https://cs.opensource.google/go/x/time/+/master:rate/rate.go;l=168;bpv=0;bpt=0

    // calculate tokens to restore
    // The duration between lim.lastEvent and r.timeToAct tells us how many tokens were reserved
    // after r was obtained. These tokens should not be restored.
    restoreTokens := float64(r.tokens) - r.limit.tokensFromDuration(r.lim.lastEvent.Sub(r.timeToAct))

If lim.LastEvent is from another reservation which is made in future compared to the current reservation we are cancelling, why do we need to take that duration into account (lim.LastEvent - r.timeToAct) ? Why couldn't we simply have restoreTokens = float64(r.tokens) here ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source