The @Timeout Annotation
Use the@Timeout annotation to define timeout behavior for tasks and workflows:
Method-Level Timeout
Apply timeouts to individual methods:Implementing WithTimeout
Create custom timeout policies by implementing theWithTimeout interface:
WithTimeout Interface
TheWithTimeout interface defines two methods:
- Returns the timeout duration in seconds
- Return
nullfor no timeout
- Returns the grace period after timeout in seconds
- Default is 0.0 (no grace period)
- Allows time for cleanup operations
Timeout Patterns
Simple Timeout
Timeout with Grace Period
- Close database connections
- Release resources
- Save partial state
- Send cleanup messages
No Timeout
Environment-Based Timeout
Workflow Method Timeouts
Set timeouts for workflow methods when dispatching them:Handling Timeout Events
When a timeout occurs, Infinitic emits timeout events:taskTimedOut- Task exceeded its timeoutmethodTimedOut- Workflow method exceeded its timeoutremoteMethodTimedOut- Child workflow method timed out
Catching Timeouts in Workflows
Timer Completion
You can manually complete timers in workflows:Timeout Configuration
Class-Level Configuration
Method Override
Best Practices
Set realistic timeouts
Set realistic timeouts
Base timeouts on actual task duration plus a buffer for variability and system load.
Use grace periods for cleanup
Use grace periods for cleanup
Always provide a grace period for tasks that need to clean up resources or save state.
Different timeouts for different operations
Different timeouts for different operations
Quick operations should have short timeouts, while batch processing might need longer ones.
Monitor timeout rates
Monitor timeout rates
High timeout rates indicate operations are consistently taking longer than expected.
Handle timeout exceptions
Handle timeout exceptions
Always handle timeout exceptions gracefully with appropriate fallback logic.
Test timeout behavior
Test timeout behavior
Test that your tasks properly handle timeouts and grace periods.
Timeout vs Retry
Timeouts and retries work together:- Task starts executing
- Timeout is reached
- Grace period allows cleanup
- Task fails with timeout error
- Retry policy determines if retry should occur
Related Topics
- Retry Policies - Configure retry behavior after timeouts
- Error Handling - Handle timeout exceptions
- CloudEvents - Monitor timeout events
- Monitoring - Track timeout metrics