Class ProviderRequestPacer
- Namespace
- MailFathom.Application.AiProviders
- Assembly
- MailFathom.Application.dll
Spaces requests to one AI provider out so a deployment never sends faster than it declared it would.
public sealed class ProviderRequestPacer
- Inheritance
-
ProviderRequestPacer
- Inherited Members
Remarks
A rate ceiling and the spend ceiling beside it bound different things and neither substitutes for the other: the budget decides how much a period may cost, this decides how quickly that cost is allowed to accumulate. It exists because a provider quota is stated per minute rather than per month, and because being refused with a rate-limit response costs an attempt, a retry, and a place in a circuit-breaker window that other work is measured in.
One instance paces one workload rather than one provider. Embedding a mailbox's passages and describing its pictures are two bulk workloads against two declared endpoints with two quotas, so each takes a pacer of its own built from its own rate and named by its own workload; sharing one would make either workload's burst spend the other's slots.
It is also not the concurrency limit. How many calls may be in flight at once is the resilience pipeline's
AiProviderInvocation budget, which is the one mechanism that owns that question and is deliberately still a
process's; a second limiter counting in-flight calls here would make two settings answer for one behaviour.
The pacing is a slot reservation rather than a token bucket that refills on a timer: a caller takes the next free slot, moves the marker forward by one interval, and waits for its own slot to arrive. The marker lives in the database rather than in this process, which is what makes the declared rate the deployment's instead of each replica's, and it is the only part of the wait that reaches one — nothing polls, nothing spins, and the wait itself is a single cancellable delay measured on the injected TimeProvider, which is what lets a test prove that the ceiling binds and then releases rather than proving it against a wall clock.
Properties
- IsUnpaced
Gets whether this pacer delays nothing, which is what a rate of zero asked for.
Methods
- Create(string, int, IProviderPaceMarker, TimeProvider)
Builds a pacer for one workload from the rate a deployment declared.
- WaitForSlotAsync(CancellationToken)
Waits until this deployment is allowed to send the paced workload's next request.