App-vNext/Polly · error · ArgumentOutOfRangeException

perTimeSpan must be a positive timespan.

Error message

perTimeSpan must be a positive timespan.

What it means

Thrown by RateLimitSyntax.RateLimit when perTimeSpan is less than or equal to TimeSpan.Zero. The window over which executions are counted must be positive for the token-bucket refill rate to be meaningful.

Source

Thrown at src/Polly/RateLimit/RateLimitSyntax.cs:37

    /// </summary>
    /// <param name="numberOfExecutions">The number of executions (call it N) permitted per timespan.</param>
    /// <param name="perTimeSpan">How often N executions are permitted.</param>
    /// <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>
    /// <returns>The policy instance.</returns>
    public static RateLimitPolicy RateLimit(
        int numberOfExecutions,
        TimeSpan perTimeSpan,
        int maxBurst)
    {
        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 RateLimitPolicy(rateLimiter);
    }

View on GitHub (pinned to d0e46bdb1e)

Solutions

  1. Set perTimeSpan to a positive TimeSpan (e.g. TimeSpan.FromSeconds(10)).
  2. Sanitize config at startup: enforce perTimeSpan > TimeSpan.Zero before building the policy.
  3. Check unit conversion (ms vs seconds) is not collapsing to zero.

Example fix

// before
var policy = Policy.RateLimit(5, TimeSpan.Zero, 5);
// after
var policy = Policy.RateLimit(5, TimeSpan.FromSeconds(10), 5);
Defensive patterns

Strategy: validation

Validate before calling

var perTimeSpan = config.GetValue<TimeSpan>("RateLimit:Window");
if (perTimeSpan <= TimeSpan.Zero) throw new InvalidOperationException("RateLimit:Window must be positive.");
var policy = Policy.RateLimit(numberOfExecutions, perTimeSpan, maxBurst);

Prevention

When it happens

Trigger: Calling Policy.RateLimit(n, TimeSpan.Zero, ...) or Policy.RateLimit(n, TimeSpan.FromSeconds(-1), ...) on the sync non-generic overload.

Common situations: perTimeSpan read from config as 0 (e.g. "0s" or missing key); misconfigured POLICY_WINDOW environment variable; unit conversion yielding zero ticks.

Related errors


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