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 fromcount > 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.
peekreports whatcheckwould decide when the identifier has thecount_distinctvalue, without writing: exceeded only when that value isn't counted and the count has reached the limit. Without the value,checkskips the rule, butpeekanswers for a value that isn't counted yet, so it reports exceeded once the count reaches the limit.- A
:logrule does the same accounting, including not adding refused values, and only skips the blocking.cost:is ignored, as for a plaincount_distinctrule. - Same Redis key and stored values as a plain
count_distinctrule.
Implementation
Rule: newadmit_counted_valueskeyword (defaultfalse, andnilmeans not set, likecount_distinctandban_for). It raises on any other non-boolean value, when it'struewithoutcount_distinct, and on:skiprules (likeban_for).Evaluator: newADMIT_SCRIPT(SISMEMBER, thenSCARD< limit, thenSADD, plus the usual TTL handling) returning{count, ttl, admitted}.build_evaluationtakes an optionalexceeded:, so admission and peek can decide it while every other path keepscount > limit. A refusal on a missing key (a limit resolving to 0) writes nothing. Peek readsSCARDandTTLin one pipeline, plusSISMEMBERwhen the identifier has the value, without writing.README.md: new "Admitting counted values" section, plus the rule table and the scripts paragraph.Rule#initializenow has 9 keywords, one overMetrics/ParameterLists. I disabled the cop inline with a reason instead of editing.rubocop_todo.ymlor packing options into a hash, because each keyword maps to aRulefield. 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.
Related Issues
- Contract: gitlab-org/quality/tooling/labkit-spec!52 (merged)
- Consumer: gitlab-org/gitlab!268802 (merged)
- Follow-up (scenario, Go and Rust): gitlab-org/quality/tooling/labkit-spec#12