{"modulePath":"github.com/gomodedev/gomode","version":"v0.6.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package ratelimit\n\nPackage ratelimit limits requests per key with token buckets and tells rejected clients when to retry.\n\nIt uses only the standard library, so the oauth protocol packages can depend on it.\n\nEvery host and Go Mode handler that answers 429 sets Retry-After with SetRetryAfter, or with WriteHeaders for a Limiter result, so clients see one rule: whole seconds, rounded up, at least one.\n\n## Functions\n\n```go\nfunc RetryAfterSeconds(wait time.Duration) int\n```\nRetryAfterSeconds is wait in whole seconds, rounded up so a retry lands after it, and at least 1.\n\n```go\nfunc SetRetryAfter(h http.Header, wait time.Duration)\n```\nSetRetryAfter sets the Retry-After header of a 429 or 503 response to wait.\n\n```go\nfunc WriteHeaders(h http.Header, result Result)\n```\nWriteHeaders describes result to the client: X-RateLimit-Limit (requests per window), X-RateLimit-Remaining, and X-RateLimit-Reset (Unix seconds when the bucket is full again), plus Retry-After on a rejection. Call it before the handler writes, on every limited response. An unlimited result writes nothing.\n\n## Types\n\n```go\ntype Limiter struct {\n\t// contains filtered or unexported fields\n}\n```\nLimiter admits requests per key at a sustained rate with a burst. The zero value is not usable; call New.\n\n```go\nfunc New(requests int, window time.Duration, burst int) *Limiter\n```\nNew returns a limiter allowing requests per window per key, up to burst at once. A requests or window of 0 or less means unlimited.\n\n```go\nfunc (l *Limiter) Allow(key string) Result\n```\nAllow consumes one request for key when its bucket has a token. When the bucket table is full, a new key waits until the next prune can free a bucket.\n\n```go\nfunc (l *Limiter) Count() int64\n```\nCount reports how many requests Allow has checked.\n\n```go\nfunc (l *Limiter) Update(requests int, window time.Duration, burst int)\n```\nUpdate changes the limit for every key. Existing buckets keep their tokens, capped at the new burst.\n\n```go\ntype Result struct {\n\tAllowed bool\n\t// Limit is the configured requests per window; 0 means unlimited.\n\tLimit int\n\t// Remaining is how many requests the key can make now.\n\tRemaining int\n\t// ResetAt is when the key's bucket is full again.\n\tResetAt time.Time\n\t// RetryAfter is how long a rejected key waits until its next request is allowed; 0 when allowed.\n\tRetryAfter time.Duration\n}\n```\nResult is the outcome of one Allow call.\n\n","path":"github.com/gomodedev/gomode/httpserver/ratelimit","name":"ratelimit","synopsis":"Package ratelimit limits requests per key with token buckets and tells rejected clients when to retry.","isRedistributable":true}
