Skip to content

Commit

Permalink
more cr suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
klaidliadon committed Sep 20, 2024
1 parent ac748ea commit 9719e45
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 7 deletions.
10 changes: 4 additions & 6 deletions limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,22 +86,20 @@ func (l *RateLimiter) OnLimit(w http.ResponseWriter, r *http.Request, key string
if limit == -1 {
return false
}
setHeader(w, l.headers.Limit, fmt.Sprintf("%d", limit))

increment, ok := getIncrement(r.Context())
if !ok {
increment = 1
}
if increment != 1 {
setHeader(w, l.headers.Increment, fmt.Sprintf("%d", increment))
}
// If the increment is 0, we do not limit
if increment == 0 {
return false
}

setHeader(w, l.headers.Limit, fmt.Sprintf("%d", limit))

if increment > 1 {
setHeader(w, l.headers.Increment, fmt.Sprintf("%d", increment))
}

l.mu.Lock()
_, rateFloat, err := l.calculateRate(key, limit)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func TestResponseHeaders(t *testing.T) {
requestsLimit: 5,
increments: []int{0, 0, 0, 0, 0, 0},
respCodes: []int{200, 200, 200, 200, 200, 200},
respLimitHeader: []string{"", "", "", "", "", ""},
respLimitHeader: []string{"5", "5", "5", "5", "5", "5"},
respRemainingHeader: []string{"", "", "", "", "", ""},
},
{
Expand Down

0 comments on commit 9719e45

Please sign in to comment.