Recipes

Enforce a sliding Rate Limit

Bound accepted Commands per execution group and time window without delaying excess work.

v0.1.0-dev.2Intermediateusecase_forge
Open source reference

Enforce a sliding Rate Limit

Use a Rate Limit when a resource accepts only a bounded number of Commands in a sliding time window. Excess Commands are rejected immediately; they do not wait for quota.

dart
registerCommand<SendVerificationCode>(
  _sendCode,
  instructions: UseCaseInstructionOverrides(
    input: UseCaseInputInstructionOverrides(
      rateLimit: UseCaseRateLimitInstructions(
        maxExecutions: 3,
        duration: const Duration(minutes: 10),
      ),
    ),
  ),
);

Give the Command an executionKey such as an account identifier. Each exact Command type and key receives its own window. Only entries that pass the earlier input filters consume quota.

Handle the boundary

maxExecutions and duration must be positive. Invalid configuration throws during registration. A rejected Command reaches onCommandRejected as rateLimitExceeded and does not enter history.

ARKTELOS

Purpose-built engineering systems for software that has to hold.