feat(redisclient): ratelimit-purpose Redis client with fail-fast defaults (S05-B plan: 1/4)

Summary

Adds the ratelimit-purpose Redis client: the RedisConfig.ratelimit proto entry (field 3), redisclient.NewRateLimitClient with the purpose's fail-fast code defaults (250 ms dial/read/write/pool timeouts and max_retries: -1, applied to unset and zero-valued fields alike), and the operator-facing configuration docs (config.example.yaml, docs/dev/configuration-reference.md). No surface constructs the client yet.

Step 1 of 4 in the S05-B plan; the limiter (Step 2), observability (Step 3), and the rate_limit block (Step 4) follow.

Spec coverage

Spec: docs/specs/S05-b-rate-limiting.md Plan: docs/plans/2026-10-05-s05b-rate-limiting.md, Step 1 (ratelimit-purpose Redis client). Rows naming a later step are owned by this plan's other steps; this MR does not cover them.

Acceptance criteria

# Criterion Tests
AC-1 NewRateLimitClient builds a ratelimit-purpose client over the infrastructure config with the code-derived metric Name redis-ratelimit, and returns the redis.New error without re-wrap so infrastructure.ErrNotConfigured matches via errors.Is TestNewRateLimitClient_MetricName, TestNewRateLimitClient_NotConfigured (unit); TestJobsCacheAndRateLimitClients_NoRegistrationCollision (integration; pins the exact name for all three purposes)
AC-2 A client built with no redis.ratelimit entry carries the fail-fast defaults (dial, read, write, and pool timeouts of 250 ms and max_retries: -1); a non-zero override replaces a default field by field; a zero-valued fail-fast field keeps the default; a test drives a check whose client times out (error outcome, read served); a check against a stalled client returns within the check budget Client-level defaults and replacement: TestNewRateLimitClient_FailFastDefaults, TestNewRateLimitClient_NonFailFastOverridesPassThrough (unit). Client-level timeout arm: TestNewRateLimitClient_FailFastOnStalledRedis (integration). Check-level arms: the error outcome with the read served is Step 3 (observe_test.go); the check-budget bound is Step 2 (ratelimit_test.go). Note: the resolved go-redis options normalize the max_retries: -1 sentinel to 0 (its internal "no retries"); the unit test asserts the resolved 0, and the integration test pins the no-retries behavior end-to-end.
AC-3 The ratelimit, cache, and jobs clients register on one Prometheus registerer without collision TestJobsCacheAndRateLimitClients_NoRegistrationCollision (integration). The pre-existing two-purpose TestCacheAndJobsClients_NoRegistrationCollision is kept unchanged.
AC-4 A rested key admits burst requests and refuses the next Step 2 (internal/ratelimit/ratelimit_integration_test.go).
AC-5 Sustained traffic at rate/period is admitted; recovery after the burst drains Step 2.
AC-6 A refused request does not advance the key's recovery Step 2.
AC-7 Check resolves each mode into the admission-check decision Step 2.
AC-8 The check counter books success/denied/error exactly once per check Step 3 (internal/ratelimit/observe_test.go).
AC-9 Redis error, check timeout, or uuid.Nil books error, logs, and the read is served with no 500 Step 3; this MR contributes only the client-level fail-fast arm (TestNewRateLimitClient_FailFastOnStalledRedis).
AC-10 Standalone, Sentinel, and Cluster topologies Step 2 (three-topology matrix).
AC-11 Keys match rate:ar:ratelimit:namespace:<namespace-uuid> Step 2.
AC-12 ratelimittest.FakeLimiter replays its scripted verdicts Step 2.
AC-13 A namespace_overrides entry replaces rate/burst for its namespace Step 2.
AC-14 An omitted mode converts to observe; the loader rejects a period below one second Step 4 (internal/config/ratelimit_test.go).
AC-15 Denial-record fields; the 64-bucket sampler caps the record and the failed-check log at 64/s per process Step 3.

Error cases

# Condition Tests
E-1 No redis stanza in the infrastructure config TestNewRateLimitClient_NotConfigured (unit)
E-2 Redis unreachable or a check times out A stalled read after a successful dial: TestNewRateLimitClient_FailFastOnStalledRedis (integration). The unreachable dial path is not covered here — go-redis makes up to five dial attempts, so only a caller deadline bounds that path, and Step 2's check budget supplies it. The check booking error, the capped log, and the served read: Step 3.
E-3 Namespace id is uuid.Nil Step 2 (the guard) and Step 3 (the error booking).
E-4 Malformed limit at construction Step 2 (ratelimit.New panic backstop).
E-5 period below one second Step 4 (loader).
E-6 Namespace override key is not a UUID Step 4 (protovalidate map-key rule).
E-7 mode omitted or empty Step 4 (conversion selects observe).
E-8 Redis failover or flush loses limiter cells Step 2 exercises the same rested-key admission path; the plan names no dedicated lost-cell test. Not covered by this MR.

Security considerations

# Concern Tests
S-1 Credentials and TLS come from the deployment-managed secret mounts through LabKit's infrastructure config; no new credential surface Dependency-owned (LabKit redis.WithInfraConfig); this MR adds no credential handling. The constructor tests build over the infra config.
S-2 The limiter key uses the resolved namespace UUID, not a caller-controlled header, principal, or IP; override keys are UUID-validated Step 2 (key grammar) and Step 4 (protovalidate).
S-3 A refusal reveals only that the namespace's rate was reached; metric labels carry neither namespace nor repository id Step 3 (observability).
S-4 Fail-open degradation is deliberate; a Redis outage does not take down the read path Step 3 (the check books error and serves the read). This MR bounds a stalled client read so the outage fails fast: TestNewRateLimitClient_FailFastOnStalledRedis. The dial path is bounded only by the caller's deadline, which Step 2's check budget supplies.

Notes

  • Plan-directed tests not tied to a spec row: TestNewRateLimitClient_MalformedDuration (the plan's "duration parse-error cases"; the per-field table for the shared mapper is the pre-existing TestOverridesToConfig_MalformedDuration).
  • The proto RedisConfig.ratelimit = 3 field and the config.example.yaml / docs/dev/configuration-reference.md entries are schema and docs, not behavior; they land with the implementation commit.

Plan and spec amendments

Review-driven corrections, per Review-driven plan and spec corrections. No acceptance criterion, contract, or test outcome changes.

  • docs/plans/2026-10-05-s05b-rate-limiting.md: the Step 1 research finding said the fail-fast defaults make "about a second per check". The dial phase is not bounded by dial_timeout: go-redis makes up to five 250 ms dial attempts with 100 ms between them, and LabKit exposes no dial-retry knob, so a dial against an unreachable host spends about 1.7 s (measured on this branch) and only a caller deadline bounds the whole check. The finding also said the dial loop takes the caller's context; the caller's wait for a queued dial takes it, but the dial itself runs on a background context, so it holds its pool turn after Check returns. The Step 1 file list now names the two negative sentinels — max_retries: -1 and -1ns on read_timeout and write_timeout — rather than three fields.
  • docs/specs/S05-b-rate-limiting.md: the same figure, in the Check budget rationale, which now says the per-field timeouts bound each attempt rather than the call, and matches the degradation paragraph's wording.
  • docs/dev/configuration-reference.md and config.example.yaml: the same claims, corrected in the operator-facing text, plus the per-field effect of a negative value: -1ns removes only that field's timeout, any other negative read_timeout or write_timeout removes the socket deadline and the caller's deadline with it, and a negative dial_timeout or pool_timeout fails at once. The dial clause now says a caller's deadline ends the caller's wait while the dial continues on a background context and holds its pool turn. Both files also drop the forward reference to a later step's check, its budget, and its fail-open posture, and state the as-is consequence instead.
  • internal/redisclient/client.go: the same claims in the fail-fast defaults comment, the overridesToConfig positivity comment, and the ContextTimeoutEnabled trailing comment, which said the flag makes a call deadline bound the socket read.
  • internal/redisclient/client_test.go: the max_retries -1 row is renamed to the resolved outcome it actually asserts. It cannot fail while the mapper's max_retries branch is dropped, because the default resolves to the same value; the non-zero max_retries row is what guards that branch. The -1s row is renamed because -1s is not a sentinel, -1ns rows now pin the sentinel's resolved 0 for both read_timeout and write_timeout, and the shared assertion message names what ContextTimeoutEnabled does rather than claiming a deadline reaches the socket.

No e2e scenario added

Nothing in this step is observable from outside the service: no surface constructs the client, and the redis.ratelimit stanza is accepted but not read. No customer journey changes, so no scenario row is added or affected.

No docs/user change

The only customer-visible surface this step could touch is configuration, documented in the operator-facing configuration reference and example. docs/user/** moves with the rate_limit block and the first mount (Steps 2–4 and the S30 mount plan), where customer-visible behavior first exists.

Related to #1181

Edited by Moaz Khalifa

Merge request reports

Loading
Loading