App-vNext/Polly · error · ArgumentOutOfRangeException

The number of executions per timespan must be positive.

Error message

The number of executions per timespan must be positive.

What it means

Thrown by RateLimitTResultSyntax.RateLimit<TResult> when the derived per-token interval onePer (perTimeSpan.Ticks / numberOfExecutions) rounds down to zero. The generic overload computes the same refill interval as the non-generic one and guards against an unusable sub-tick interval.

Source

Thrown at src/Polly/RateLimit/RateLimitTResultSyntax.cs:85

        {
            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 RateLimitPolicy<TResult>(rateLimiter, retryAfterFactory);
    }
}

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Increase perTimeSpan so each token represents at least 1 tick (100ns).
  2. Reduce numberOfExecutions to keep the per-token interval positive.
  3. Confirm the time unit and tick math at config time.

Example fix

// before
var policy = Policy.RateLimit<MyResult>(10000, TimeSpan.FromTicks(1), 10000, factory);
// after
var policy = Policy.RateLimit<MyResult>(10000, TimeSpan.FromSeconds(1), 10000, factory);
Defensive patterns

Strategy: validation

Validate before calling

if (perTimeSpan.Ticks / numberOfExecutions < 1) throw new InvalidOperationException("perTimeSpan too short for the configured execution count.");
var policy = Policy.RateLimit<TResult>(numberOfExecutions, perTimeSpan, maxBurst, retryAfterFactory);

Prevention

When it happens

Trigger: A combination where perTimeSpan.Ticks / numberOfExecutions <= 0 in the generic overload — e.g. very high execution count against a sub-tick window.

Common situations: High-throughput generic policies; unit mismatch between config and TimeSpan; integer division flooring to zero.

Related errors


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