Skip to content

Commit

Permalink
Handle cases here the server time is set to the past, such as dayligh…
Browse files Browse the repository at this point in the history
…t savings time (#43)

* Calling back the server time can cause an exception in the flow limiting mechanism

If the server time is adjusted back, we need to update the startTime of the bucket and reset the lastTick to 0

* Handle server time going backwards

---------

Co-authored-by: FishJone <314130446@qq.com>
  • Loading branch information
sethvargo and yujiangjiang committed Mar 25, 2024
1 parent 54af081 commit e3fb1a5
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions memorystore/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,14 +293,22 @@ func (b *bucket) take() (tokens uint64, remaining uint64, reset uint64, ok bool,
// Capture the current request time, current tick, and amount of time until
// the bucket resets.
now := fasttime.Now()

b.lock.Lock()
defer b.lock.Unlock()

// If the current time is before the start time, it means the server clock was
// reset to an earlier time. In that case, rebase to 0.
if now < b.startTime {
b.startTime = now
b.lastTick = 0
}

currTick := tick(b.startTime, now, b.interval)

tokens = b.maxTokens
reset = b.startTime + ((currTick + 1) * uint64(b.interval))

b.lock.Lock()
defer b.lock.Unlock()

// If we're on a new tick since last assessment, perform
// a full reset up to maxTokens.
if b.lastTick < currTick {
Expand Down

0 comments on commit e3fb1a5

Please sign in to comment.