|
|
@@ -435,10 +435,12 @@ void dispatch(ServerDeps& d, const httplib::Request& req, httplib::Response& res
|
|
|
// Only where a caller exists. An empty address means nothing trustworthy
|
|
|
// resolved, and bucketing those together would charge every such request
|
|
|
// to one shared counter - a throttle keyed on "" rather than on a caller.
|
|
|
- if (policies[idx]->rateLimitPerCallerPerMin > 0 && !clientIp.empty() &&
|
|
|
- !rateLimiter().allow(
|
|
|
- functions::fnCallerThrottleBucket(project, functionId, clientIp),
|
|
|
- policies[idx]->rateLimitPerCallerPerMin)) {
|
|
|
+ const RateLimiter::Decision callerDecision =
|
|
|
+ (policies[idx]->rateLimitPerCallerPerMin > 0 && !clientIp.empty())
|
|
|
+ ? rateLimiter().check(functions::fnCallerThrottleBucket(project, functionId, clientIp),
|
|
|
+ policies[idx]->rateLimitPerCallerPerMin)
|
|
|
+ : RateLimiter::Decision{true, false};
|
|
|
+ if (!callerDecision.allowed) {
|
|
|
res.set_header("Retry-After", std::to_string(kRetryAfterRateLimitedSec));
|
|
|
// WHICH ceiling fired is recorded for the OPERATOR and withheld from
|
|
|
// the caller, because the two audiences need opposite things.
|
|
|
@@ -452,23 +454,57 @@ void dispatch(ServerDeps& d, const httplib::Request& req, httplib::Response& res
|
|
|
// apart - asked for by a consumer who could see the symptom and not
|
|
|
// the cause. The address is already in our access log, so naming it
|
|
|
// here discloses nothing new.
|
|
|
- spdlog::info("fn throttle: PER-CALLER ceiling {} reached for {}/{} by {}",
|
|
|
- policies[idx]->rateLimitPerCallerPerMin, project, functionId, clientIp);
|
|
|
+ // ONCE PER CALLER PER MINUTE, not once per refused request.
|
|
|
+ //
|
|
|
+ // A consumer argued this line was safe to emit per refusal because its
|
|
|
+ // count is "bounded by distinct abusive callers rather than by request
|
|
|
+ // volume". That premise does not hold: a refused caller that keeps
|
|
|
+ // sending is refused on EVERY request, so one determined address
|
|
|
+ // produces one line per request exactly as the per-function line does.
|
|
|
+ // Their conclusion was right for the per-function case and the
|
|
|
+ // reasoning does not survive being applied to this one, so both are
|
|
|
+ // throttled the same way.
|
|
|
+ //
|
|
|
+ // Nothing is lost by it: the VOLUME of refusals is already one line
|
|
|
+ // per 429 in the access log, so the journal only has to answer the
|
|
|
+ // question the access log cannot - WHICH ceiling fired. Logged at
|
|
|
+ // warn, not info: a caller hitting its own ceiling is the one an
|
|
|
+ // operator may need to go and find.
|
|
|
+ if (callerDecision.firstRefusalInWindow)
|
|
|
+ spdlog::warn("fn throttle: PER-CALLER ceiling {}/min reached for {}/{} by {} "
|
|
|
+ "(further refusals this minute are not logged; count them in the "
|
|
|
+ "access log)",
|
|
|
+ policies[idx]->rateLimitPerCallerPerMin, project, functionId, clientIp);
|
|
|
sendJson(res, 429,
|
|
|
errorBody("rate_limited", "this function's per-minute limit is exhausted"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- if (policies[idx]->rateLimitPerMin > 0 &&
|
|
|
- !rateLimiter().allow(functions::fnHttpThrottleBucket(project, functionId),
|
|
|
- policies[idx]->rateLimitPerMin)) {
|
|
|
+ const RateLimiter::Decision fnDecision =
|
|
|
+ policies[idx]->rateLimitPerMin > 0
|
|
|
+ ? rateLimiter().check(functions::fnHttpThrottleBucket(project, functionId),
|
|
|
+ policies[idx]->rateLimitPerMin)
|
|
|
+ : RateLimiter::Decision{true, false};
|
|
|
+ if (!fnDecision.allowed) {
|
|
|
res.set_header("Retry-After", std::to_string(kRetryAfterRateLimitedSec));
|
|
|
// The sibling of the line above, and worded to be greppable as a pair:
|
|
|
// this one means the function is globally saturated rather than one
|
|
|
// caller misbehaving, which is the distinction that decides the fix.
|
|
|
- spdlog::info("fn throttle: PER-FUNCTION ceiling {} reached for {}/{} (caller {})",
|
|
|
- policies[idx]->rateLimitPerMin, project, functionId,
|
|
|
- clientIp.empty() ? "unresolved" : clientIp.c_str());
|
|
|
+ // ONCE PER FUNCTION PER MINUTE. Global saturation refuses EVERY caller,
|
|
|
+ // so volume is precisely when this fires and a line per refusal would
|
|
|
+ // bury the per-caller lines that identify the cause - which is the
|
|
|
+ // thing this pair exists to distinguish. The first line is the alert.
|
|
|
+ //
|
|
|
+ // The caller is named for context only and is NOT the culprit here:
|
|
|
+ // under saturation it is whichever request happened to arrive first.
|
|
|
+ // Saying so in the line, because an operator reading an address next
|
|
|
+ // to a throttle message will reasonably assume it is to blame.
|
|
|
+ if (fnDecision.firstRefusalInWindow)
|
|
|
+ spdlog::warn("fn throttle: PER-FUNCTION ceiling {}/min reached for {}/{} - the "
|
|
|
+ "function is saturated, so every caller is being refused; first "
|
|
|
+ "refused caller this minute was {} (not necessarily the cause)",
|
|
|
+ policies[idx]->rateLimitPerMin, project, functionId,
|
|
|
+ clientIp.empty() ? std::string("unresolved") : clientIp);
|
|
|
sendJson(res, 429,
|
|
|
errorBody("rate_limited", "this function's per-minute limit is exhausted"));
|
|
|
return;
|