Documentation Index
Fetch the complete documentation index at: https://mintlify.com/App-vNext/Polly/llms.txt
Use this file to discover all available pages before exploring further.
Timeout Strategy
The timeout proactive resilience strategy cancels the execution if it does not complete within the specified timeout period. If the execution is canceled by the timeout strategy, it throws aTimeoutRejectedException.
When to Use Timeout
Use the timeout strategy when:- Operations should not take longer than a specific duration
- You want to prevent indefinite waiting for responses
- Dealing with potentially slow or unresponsive services
- Implementing SLA requirements with strict time limits
- Protecting against resource exhaustion from long-running operations
Installation
Usage
Basic Timeout
Using TimeoutStrategyOptions
Dynamic Timeout
Timeout with Events
Configuration Options
Defines a fixed period within which the delegate should complete, otherwise it will be cancelled.
Dynamically calculates the timeout period using runtime information. If set, the
Timeout property is ignored.Invoked after the timeout occurred, just before throwing
TimeoutRejectedException.Important Considerations
Respecting Cancellation Tokens
The timeout strategy relies on co-operative cancellation. Your callbacks must honor the cancellation token:If the cancellation token is not respected, the callback will continue executing after the timeout, and the strategy will have to wait for it to complete before throwing
TimeoutRejectedException.Best Practices
Always use the provided cancellation token
Always use the provided cancellation token
Pass the cancellation token provided by Polly to all async operations. This ensures proper timeout behavior.
Set appropriate timeout values
Set appropriate timeout values
- Too short: operations fail unnecessarily
- Too long: defeats the purpose of timeout
Use dynamic timeouts for varying scenarios
Use dynamic timeouts for varying scenarios
Use
TimeoutGenerator when different operations or contexts require different timeout values.Combine with retry for comprehensive resilience
Combine with retry for comprehensive resilience
Place timeout inside retry to ensure each retry attempt has its own timeout:
Log timeout events
Log timeout events
Use
OnTimeout to log or send metrics when timeouts occur. This helps identify slow operations.Examples
HTTP Request with Timeout
Database Query with Timeout
Per-Operation Dynamic Timeouts
Timeout with Retry
Overall Timeout for Retries
OnTimeout vs Try-Catch
OnTimeout is called before the exception is thrown. This is useful when combining with retry strategy, as the retry might handle the exception. If you use try-catch, you might not catch the exception if retry succeeds on a subsequent attempt.What happens if my operation ignores the cancellation token?
What happens if my operation ignores the cancellation token?
The timeout strategy will wait for your operation to complete before throwing
TimeoutRejectedException. This defeats the purpose of the timeout. Always respect the cancellation token in your callbacks.Can I use different timeouts for different operations?
Can I use different timeouts for different operations?
Yes! Use
TimeoutGenerator to dynamically calculate timeouts based on context properties, operation key, or any other runtime information.Does timeout affect the original cancellation token?
Does timeout affect the original cancellation token?
The timeout strategy wraps the incoming cancellation token with a new one. If the original token is canceled, the timeout strategy honors it without throwing
TimeoutRejectedException.Should timeout be inside or outside retry?
Should timeout be inside or outside retry?
It depends:
- Inside retry (retry → timeout): Each attempt gets its own timeout
- Outside retry (timeout → retry): The entire retry sequence must complete within the timeout