feat(rate_limit): add admit_counted_values to count_distinct rules

Description

Adds admit_counted_values, an opt-in for count_distinct rules that counts only the values the rule admits.

Why: a plain count_distinct rule adds every checked value to its set, even the ones it blocks. After the first block, the count stays over the limit for the rest of the window, so values counted earlier are blocked too. That's right for "block after N distinct values", but not for a cap on how many distinct values can be admitted per window, where an admitted value should keep passing.

GitLab needs the second behavior to cap how many distinct new external email addresses a top-level namespace can add as Service Desk participants per day. An address added earlier in the day must still be addable to other tickets after the cap is reached: gitlab-org/gitlab!268802 (merged) (draft, waiting on this release). The caller can't build this on top of the current API: it only sees the count, and a blocked value that was added to the set looks the same as one that was admitted.

Behavior with admit_counted_values: true:

  • A value already in the set is allowed and leaves the count unchanged, even when the set is full.
  • A new value is added only while the count is below the limit. Otherwise the request is blocked and the value is not added, so a refused value stays refused on retry and the count does not grow past the limit.
  • A blocked request reports the set's count and RateLimit-Remaining: 0. The admission decides the block, the same way a ban decides it apart from count > limit.
  • If the limit is lowered during a window, values already in the set are still allowed, and new values are blocked until the window ends.
  • peek reports what check would decide when the identifier has the count_distinct value, without writing: exceeded only when that value isn't counted and the count has reached the limit. Without the value, check skips the rule, but peek answers for a value that isn't counted yet, so it reports exceeded once the count reaches the limit.
  • A :log rule does the same accounting, including not adding refused values, and only skips the blocking. cost: is ignored, as for a plain count_distinct rule.
  • Same Redis key and stored values as a plain count_distinct rule.
Implementation
  • Rule: new admit_counted_values keyword (default false, and nil means not set, like count_distinct and ban_for). It raises on any other non-boolean value, when it's true without count_distinct, and on :skip rules (like ban_for).
  • Evaluator: new ADMIT_SCRIPT (SISMEMBER, then SCARD < limit, then SADD, plus the usual TTL handling) returning {count, ttl, admitted}. build_evaluation takes an optional exceeded:, so admission and peek can decide it while every other path keeps count > limit. A refusal on a missing key (a limit resolving to 0) writes nothing. Peek reads SCARD and TTL in one pipeline, plus SISMEMBER when the identifier has the value, without writing.
  • README.md: new "Admitting counted values" section, plus the rule table and the scripts paragraph.
  • Rule#initialize now has 9 keywords, one over Metrics/ParameterLists. I disabled the cop inline with a reason instead of editing .rubocop_todo.yml or packing options into a hash, because each keyword maps to a Rule field. Happy to change that if you prefer another approach.

Contract: this is a contract change, pinned first in gitlab-org/quality/tooling/labkit-spec!52 (merged) under "Pinned ahead of a scenario". That MR should merge before this one. Go and Rust don't implement the mode yet. A conformance scenario follows once this is released and the Ruby server pin is bumped. Both are tracked in gitlab-org/quality/tooling/labkit-spec#12.

Tests: spec/labkit/rate_limit and spec/labkit/rate_limit_spec.rb pass (574 examples, 42 new) and RuboCop is clean. Locally, spec/labkit/httpclient_publisher_spec.rb fails on master too, because its test server can't start in my environment.

Edited by Mario Celi

Merge request reports

Loading
Loading