Skip to content

limiter: pass request context to LimitCounter I/O - #65

Open
team-humaki wants to merge 1 commit into
go-chi:masterfrom
team-humaki:limiter-counter-context
Open

team-humaki wants to merge 1 commit into
go-chi:masterfrom
team-humaki:limiter-counter-context

Conversation

@team-humaki

Copy link
Copy Markdown

Fixes #62.

Add LimitCounterContext next to LimitCounter. When a backend implements it, OnLimit / StatusContext pass context.WithoutCancel(r.Context()) into increment/get so trace values propagate but a canceled request cannot skip the count.

Existing LimitCounter implementations keep working via type assertion. The in-memory counter implements both.

Add LimitCounterContext so network backends (Redis) can receive the
request context for tracing. The limiter uses context.WithoutCancel so
a client disconnect cannot skip the increment.

Fixes go-chi#62
@github-actions

Copy link
Copy Markdown

Benchmark Results

goos: linux
goarch: amd64
pkg: github.com/go-chi/httprate
cpu: INTEL(R) XEON(R) PLATINUM 8573C
               │ master.txt  │            pr.txt             │
               │   sec/op    │   sec/op     vs base          │
LocalCounter-4   32.52m ± 1%   32.59m ± 1%  ~ (p=0.971 n=10)

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

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

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.

Add context.Context to LimitCounter methods that may invoke network calls

1 participant