Skip to content

Commit

Permalink
chore: sliding window rate limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
zenghur committed Nov 26, 2020
1 parent c42d403 commit 6eebb6f
Showing 1 changed file with 3 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ return limit - count
`
)

var errLimitHit = errors.New("limit hit")

// IsLimitHit detects a limit condition.
func IsLimitHit(err error) bool {
return errors.Is(err, errLimitHit)
}

func scriptDigest() (string, error) {
s := sha1.New()
_, err := io.WriteString(s, script)
Expand Down Expand Up @@ -87,10 +80,10 @@ func (sl *SlidingWindowRateLimiter) Allow() (bool, error) {
}
switch v := ret.(type) {
case int64:
if v == 0 {
return false, errLimitHit
if v <= 0 {
return false, errors.New("limit reached")
}
return v > 0, nil
return true, nil
default:
return false, fmt.Errorf("sliding window rate limiter err: %#v, key = %s, window = %s, limit = %d", ret, sl.key, sl.window, sl.limit)
}
Expand Down

0 comments on commit 6eebb6f

Please sign in to comment.