Skip to content
Merged
37 changes: 37 additions & 0 deletions idempotency.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,39 @@ type IdempotencyOption = idempotency.Option
// type as middleware.IdempotencyStore.
type IdempotencyStore = idempotency.Store

// IdempotencyMemoryOption configures an in-memory idempotency store. It is the
// same type as middleware.MemoryIdempotencyOption, so these options can be
// passed to middleware.NewMemoryIdempotencyStore.
type IdempotencyMemoryOption = idempotency.MemoryOption

// ErrIdempotencyQuotaExceeded is what the in-memory store returns, wrapped,
// when a principal already holds as many claims and stored responses as its
// quota allows. The idempotency middleware answers it with 429.
var ErrIdempotencyQuotaExceeded = idempotency.ErrQuotaExceeded

// IdempotencyMemoryMaxBytes caps the approximate bytes the in-memory store's
// responses take across all principals (default 256 MiB). Past it the least
// recently used response is evicted.
func IdempotencyMemoryMaxBytes(n int64) IdempotencyMemoryOption {
return idempotency.WithMaxBytes(n)
}

// IdempotencyMemoryMaxEntriesPerPrincipal caps the claims and stored responses
// one principal may hold in the in-memory store. The default is a tenth of the
// store-wide entry cap. A principal at the cap gets 429 for new keys until its
// earlier responses expire, rather than pushing out other principals'
// responses. A negative value removes the cap.
func IdempotencyMemoryMaxEntriesPerPrincipal(n int) IdempotencyMemoryOption {
return idempotency.WithMaxEntriesPerPrincipal(n)
}

// IdempotencyMemoryMaxBytesPerPrincipal caps the approximate bytes one
// principal's responses may take in the in-memory store. The default is a
// tenth of the store-wide byte cap. A negative value removes the cap.
func IdempotencyMemoryMaxBytesPerPrincipal(n int64) IdempotencyMemoryOption {
return idempotency.WithMaxBytesPerPrincipal(n)
}

// IdempotencyBackend selects the store. Without it, every route shares one
// process-wide in-memory store, which is right for a single instance and
// wrong behind a load balancer: use a shared store there.
Expand All @@ -41,6 +74,10 @@ func IdempotencyBackend(store IdempotencyStore) IdempotencyOption {
// other status is stored and replayed until the TTL (24h by default).
// - A response larger than middleware.IdempotencyMaxResponse (1 MiB) is sent
// in full and replayed without its body, marked Idempotent-Truncated: true.
// - A principal that already holds its quota of stored responses (in the
// default in-memory store, a tenth of the store) gets 429 for a new key,
// with Retry-After, and the handler does not run. One caller cannot push
// another's stored responses out of the store.
// - A request with no principal is not deduplicated: it runs as if it had
// no key, and its response carries Idempotency-Skipped: anonymous. Every
// anonymous caller shares the empty principal, so one could otherwise be
Expand Down
13 changes: 10 additions & 3 deletions interceptors/interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,9 @@ var (
// RateLimit creates a rate limit interceptor with a custom checker.
RateLimit = router.RateLimit

// RateLimitByIP creates a rate limit interceptor keyed by client IP.
// RateLimitByIP creates a rate limit interceptor keyed by client IP. The
// key is the address from RemoteAddr without its port, so every
// connection from one client shares a bucket.
RateLimitByIP = router.RateLimitByIP
)

Expand All @@ -202,10 +204,15 @@ var (
// ---------------------------------------------------------------------------

var (
// AllowIPs only allows specific IP addresses.
// AllowIPs only allows the listed clients. Entries are single addresses
// or CIDR ranges, IPv4 or IPv6, matched against the address in
// RemoteAddr. X-Forwarded-For is not read. It panics on an invalid entry.
AllowIPs = router.AllowIPs

// DenyIPs blocks specific IP addresses.
// DenyIPs blocks the listed clients. Entries are single addresses or CIDR
// ranges, IPv4 or IPv6, matched against the address in RemoteAddr. A
// request whose address cannot be parsed is blocked too. It panics on an
// invalid entry.
DenyIPs = router.DenyIPs
)

Expand Down
Loading
Loading