{"record":{"id":"96328b2fc78c3b92","repo":"App-vNext/Polly","slug":"pertimespan-must-be-a-positive-timespan-96328b","errorCode":null,"errorMessage":"perTimeSpan must be a positive timespan.","messagePattern":"perTimeSpan must be a positive timespan\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs","lineNumber":73,"sourceCode":"    /// <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).\n    /// This equates to the bucket-capacity of a token-bucket implementation.</param>\n    /// <param name=\"retryAfterFactory\">An (optional) factory to use to express retry-after back to the caller, when an operation is rate-limited.\n    /// <remarks>If null, a <see cref=\"RateLimitRejectedException\"/> with property <see cref=\"RateLimitRejectedException.RetryAfter\"/> will be thrown to indicate rate-limiting.</remarks></param>\n    /// <returns>The policy instance.</returns>\n    public static AsyncRateLimitPolicy<TResult> RateLimitAsync<TResult>(\n        int numberOfExecutions,\n        TimeSpan perTimeSpan,\n        int maxBurst,\n        Func<TimeSpan, Context, TResult>? retryAfterFactory)\n    {\n        if (numberOfExecutions < 1)\n        {\n            throw new ArgumentOutOfRangeException(nameof(numberOfExecutions), numberOfExecutions, $\"{nameof(numberOfExecutions)} per timespan must be an integer greater than or equal to 1.\");\n        }\n\n        if (perTimeSpan <= TimeSpan.Zero)\n        {\n            throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, $\"{nameof(perTimeSpan)} must be a positive timespan.\");\n        }\n\n        if (maxBurst < 1)\n        {\n            throw new ArgumentOutOfRangeException(nameof(maxBurst), maxBurst, $\"{nameof(maxBurst)} must be an integer greater than or equal to 1.\");\n        }\n\n        var onePer = TimeSpan.FromTicks(perTimeSpan.Ticks / numberOfExecutions);\n\n        if (onePer <= TimeSpan.Zero)\n        {\n            throw new ArgumentOutOfRangeException(nameof(perTimeSpan), perTimeSpan, \"The number of executions per timespan must be positive.\");\n        }\n\n        IRateLimiter rateLimiter = new LockFreeTokenBucketRateLimiter(onePer, maxBurst);\n\n        return new AsyncRateLimitPolicy<TResult>(rateLimiter, retryAfterFactory);\n    }","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/App-vNext/Polly/blob/d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac/src/Polly/RateLimit/AsyncRateLimitTResultSyntax.cs#L55-L91","documentation":"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.","triggerScenarios":"Calling Policy.RateLimitAsync<TResult>(n, TimeSpan.Zero, burst, factory) or with a negative TimeSpan. The retryAfterFactory does not influence this check.","commonSituations":"perTimeSpan from a config setting that is unset (default(TimeSpan)), mis-converted units producing zero, or a negative result of a duration computation.","solutions":["Supply a positive TimeSpan, e.g. TimeSpan.FromSeconds(1).","Validate perTimeSpan > TimeSpan.Zero before constructing the policy.","Verify the unit (ticks/ms/s) of the source setting.","Default optional config to a known-good positive value."],"exampleFix":"// before\nvar p = Policy.RateLimitAsync<int>(10, TimeSpan.Zero, 10, null);\n\n// after\nvar p = Policy.RateLimitAsync<int>(10, TimeSpan.FromSeconds(1), 10, null);","handlingStrategy":"validation","validationCode":"if (perTimeSpan <= TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(perTimeSpan));\nvar p = Policy.RateLimitAsync<TResult>(n, perTimeSpan, maxBurst, retryAfterFactory);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the unit of the source setting before converting to TimeSpan.","Default optional perTimeSpan to a sane positive value.","Validate the triple at startup."],"tags":["argumentoutofrange","ratelimit","config","validation","timespan","tresult","legacy-api"],"backgroundTag":null,"analyzedSha":"d0e46bdb1ee11ea50d0e4b6846d2633d6bc09bac","analyzedAt":"2026-08-13T16:36:01.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}