Skip to content

Commit

Permalink
cr suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Sep 20, 2024
1 parent 7eaea8f commit ac748ea
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
4 changes: 1 addition & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const (
requestLimitKey
)

const _NoLimit = -1

func WithIncrement(ctx context.Context, value int) context.Context {
return context.WithValue(ctx, incrementKey, value)
}
Expand All @@ -21,7 +19,7 @@ func getIncrement(ctx context.Context) (int, bool) {
}

func WithNoLimit(ctx context.Context) context.Context {
return context.WithValue(ctx, requestLimitKey, _NoLimit)
return context.WithValue(ctx, requestLimitKey, -1)
}

func WithRequestLimit(ctx context.Context, value int) context.Context {
Expand Down
8 changes: 4 additions & 4 deletions limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,20 +78,20 @@ func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string
if !ok {
limit = l.requestLimit
}
// If the limit is set to 0, we are always over limit
// If the limit is set to 0, we always limit
if limit == 0 {
return true
}
// If the limit is set to -1, we are never over limit
if limit == _NoLimit {
// If the limit is set to -1, there is no limit
if limit == -1 {
return false
}

increment, ok := getIncrement(r.Context())
if !ok {
increment = 1
}
// If the increment is 0, we are always on limit
// If the increment is 0, we do not limit
if increment == 0 {
return false
}
Expand Down

0 comments on commit ac748ea

Please sign in to comment.