Limit each client's requests to the image routes (closes #236)
check / check (push) Waiting to run
check / check (push) Waiting to run
The new setting image_requests_per_minute (PIXA_IMAGE_REQUESTS_PER_MINUTE), default 600, caps the GET and HEAD requests one client sends to /v1/image/ and /v1/e/ per minute; the next is answered 429 with Retry-After and the image routes' JSON error body. It uses the login limit's RateLimit middleware, so clients are told apart the same way, behind the CORS middleware, so a preflight is not counted. 0 turns it off, as script/loadtest does; a negative or non-integer value stops startup. TestEnvironmentSetsEveryKey gains the new variable, under the standing approval in #64. To stay within the linters' limits, the sentry_dsn check moves unchanged into validateSentryDSN, and the cache_max_bytes explicit flag is set in one line. Model: opus-5-5
This commit was merged in pull request #237.
This commit is contained in:
@@ -206,6 +206,16 @@ func parseExpires(query url.Values) (time.Time, error) {
|
||||
return time.Unix(exp, 0), nil
|
||||
}
|
||||
|
||||
// HandleTooManyRequests answers 429 Too Many Requests with the JSON error
|
||||
// body the image routes send for their other errors. The image routes' rate
|
||||
// limit calls it for a request over the limit.
|
||||
func (s *Handlers) HandleTooManyRequests() http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, _ *http.Request) {
|
||||
s.respondError(w, "too many requests, try again later",
|
||||
http.StatusTooManyRequests)
|
||||
}
|
||||
}
|
||||
|
||||
// respondImageError maps image retrieval errors to HTTP responses.
|
||||
func (s *Handlers) respondImageError(
|
||||
w http.ResponseWriter, req *imgcache.ImageRequest, err error,
|
||||
|
||||
Reference in New Issue
Block a user