Skip to content

feat: add Skip middleware; improve docs#56

Open
lrstanley wants to merge 1 commit into
go-chi:masterfrom
lrstanley:feature/skip-misc-cleanup
Open

feat: add Skip middleware; improve docs#56
lrstanley wants to merge 1 commit into
go-chi:masterfrom
lrstanley:feature/skip-misc-cleanup

Conversation

@lrstanley

@lrstanley lrstanley commented Mar 29, 2026

Copy link
Copy Markdown

Changes in this PR

  • Added httprate.Skip, which is a simpler (and more obvious) shorthand for setting the increment to 0 in context, to have endpoints designed to let API users see what their rate limit is (specifically when the key isn't endpoint specific, for example), without incrementing it. i.e., an endpoint that still sets the associated headers, without incrementing the rate limit.
    • Added test that covers this new flow.
  • Added documentation/comments to most functions, as there were almost none before.
    • Uses the newer linkable syntax for inter-package and external-package types (in most IDEs, allows hovering over the function/type, and making those type references clickable).
  • Adjusted some of the logic to be modernized for Go 1.24 -- loop vars, range over int, time.Time.Equals for more accurate time equality checks, etc.
  • Fixed odd formatting of the go.mod file.
  • More stdlib constant usage vs generic ints, e.g. 429 vs http.StatusTooManyRequests.

Notes to Reviewer

All tests pass locally with:

go test -v -race -count 10 -cpu 1,4 ./...

Signed-off-by: Liam Stanley <liam@liam.sh>
@github-actions

Copy link
Copy Markdown

Benchmark Results

goos: linux
goarch: amd64
pkg: github.com/go-chi/httprate
cpu: AMD EPYC 7763 64-Core Processor                
               │ master.txt  │            pr.txt             │
               │   sec/op    │   sec/op     vs base          │
LocalCounter-4   32.57m ± 4%   32.84m ± 1%  ~ (p=0.529 n=10)

               │  master.txt  │             pr.txt             │
               │     B/op     │     B/op      vs base          │
LocalCounter-4   2.845Mi ± 0%   2.847Mi ± 0%  ~ (p=0.280 n=10)

               │ master.txt  │            pr.txt             │
               │  allocs/op  │  allocs/op   vs base          │
LocalCounter-4   121.6k ± 0%   121.6k ± 0%  ~ (p=0.289 n=10)

Comment thread httprate.go
Comment on lines +184 to +191
// rl := httprate.Limit(100, time.Minute)
// r.With(rl).Get(...) // Will be rate limited.
// r.With(httprate.Skip, rl).Get(...) // Will not be rate limited, but still sets appropriate headers.
func Skip(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
next.ServeHTTP(w, r.WithContext(WithIncrement(r.Context(), 0)))
})
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the idea. Let me think about this some more and I'll see if I can include it in the future releases 👍

@VojtechVitek VojtechVitek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split "improve docs" as a separate PR next time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants