App-vNext/Polly · error · ArgumentOutOfRangeException

perTimeSpan must be a positive timespan.

Error message

perTimeSpan must be a positive timespan.

What it means

Thrown by the typed RateLimitAsync<TResult> overload when perTimeSpan is less than or equal to TimeSpan.Zero. Same guard and rationale as the factory-less variant: the token-bucket refill window must be a positive duration.

Source

Thrown at src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs:73

    /// <param name="maxBurst">The maximum number of executions that will be permitted in a single burst (for example if none have been executed for a while).
    /// This equates to the bucket-capacity of a token-bucket implementation.</param>
    /// <param name="retryAfterFactory">An (optional) factory to use to express retry-after back to the caller, when an operation is rate-limited.
    /// <remarks>If null, a <see cref="RateLimitRejectedException"/> with property <see cref="RateLimitRejectedException.RetryAfter"/> will be thrown to indicate rate-limiting.</remarks></param>
    /// <returns>The policy instance.</returns>
    public static AsyncRateLimitPolicy<TResult> RateLimitAsync<TResult>(
        int numberOfExecutions,
        TimeSpan perTimeSpan,
        int maxBurst,
        Func<TimeSpan, Context, TResult>? retryAfterFactory)
    {
        if (numberOfExecutions < 1)
        {
            throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1.");
        }

        if (perTimeSpan <= TimeSpan.Zero)
        {
            throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $"{nameof(perTimeSpan)} must be a positive timespan.");
        }

        if (maxBurst < 1)
        {
            throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $"{nameof(maxBurst)} must be an integer greater than or equal to 1.");
        }

        var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions);

        if (onePer <= TimeSpan.Zero)
        {
            throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, "The number of executions per timespan must be positive.");
        }

        IRateLimiter rateLimiter = new LockFreeTokenBucketRateLimiter(onePer, maxBurst);

        return new AsyncRateLimitPolicy<TResult>(rateLimiter, retryAfterFactory);
    }

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Supply a positive TimeSpan, e.g. TimeSpan.FromSeconds(1).
  2. Validate perTimeSpan > TimeSpan.Zero before constructing the policy.
  3. Verify the unit (ticks/ms/s) of the source setting.
  4. Default optional config to a known-good positive value.

Example fix

// before
var p = Policy.RateLimitAsync<int>(10, TimeSpan.Zero, 10, null);

// after
var p = Policy.RateLimitAsync<int>(10, TimeSpan.FromSeconds(1), 10, null);
Defensive patterns

Strategy: validation

Validate before calling

if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan));
var p = Policy.RateLimitAsync<TResult>(n, perTimeSpan, maxBurst, retryAfterFactory);

Prevention

When it happens

Trigger: Calling Policy.RateLimitAsync<TResult>(n, TimeSpan.Zero, burst, factory) or with a negative TimeSpan. The retryAfterFactory does not influence this check.

Common situations: perTimeSpan from a config setting that is unset (default(TimeSpan)), mis-converted units producing zero, or a negative result of a duration computation.

Related errors


AI-assisted analysis of App-vNext/Polly@d0e46bdb1e (2026-08-13). Data as JSON: /api/errors/96328b2fc78c3b92. Report an issue: GitHub.