private long reserveNextTicket ( double requiredPermits , long nowMicros ) { resync ( nowMicros ) ; long microsToNextFreeTicket = Math . max ( 0 , nextFreeTicketMicros - nowMicros ) ; double storedPermitsToSpend = Math . min ( requiredPermits , this . storedPermits ) ; double freshPermits = requiredPermits - storedPermitsToSpend ; long waitMicros = storedPermitsToWaitTime ( this . storedPermits , storedPermitsToSpend ) + ( long ) ( freshPermits * stableIntervalMicros ) ; this . nextFreeTicketMicros = nextFreeTicketMicros + waitMicros ; this . storedPermits -= storedPermitsToSpend ; return microsToNextFreeTicket ; }
Reserves next ticket and returns the wait time that the caller must wait for .