Time Limiter
Time limiter for enforcing execution timeouts.
The Time Limiter pattern ensures that operations complete within a specified time limit. It provides various strategies for handling timeouts including fallbacks, retries, and statistics tracking.
Use Cases:
Prevent operations from hanging indefinitely
Enforce SLA requirements
Detect slow or unresponsive services
Set deadlines for operations
Basic usage:
val limiter = TimeLimiter.create(config = TimeLimiterConfig(timeout = 5.seconds))
val result = limiter.execute {
callSlowService()
} // throws TimeoutCancellationException if > 5sWith fallback:
val result = limiter.executeOrFallback(
fallback = { "default" }
) {
callSlowService()
}Parallel execution with timeout:
val results = limiter.executeAll(
blocks = listOf(
{ callService1() },
{ callService2() },
{ callService3() }
)
) // returns list with null for timed-out callsParameters
Configuration for time limiter behavior
Types
Factory for TimeLimiter.
Functions
Adds a listener for time limiter events.
Executes an operation with a timeout and fallback.
Executes an operation with a timeout, returning null if it times out.
Resets all statistics counters.
Gets the current statistics.
Inherited functions
Adapts this TimeLimiter into a Policy.