Bulkhead
Bulkhead pattern implementation for concurrency isolation.
Limits the number of concurrent calls to protect shared resources from overload. When the maximum number of concurrent calls is reached, additional calls are either queued (up to BulkheadConfig.maxWaitingCalls) or rejected immediately.
Basic usage:
val bulkhead = Bulkhead.create(config = BulkheadConfig(maxConcurrentCalls = 5))
val result = bulkhead.execute {
callExternalService()
}Content copied to clipboard
With waiting queue:
val bulkhead = Bulkhead.create(config = BulkheadConfig(
maxConcurrentCalls = 10,
maxWaitingCalls = 20,
maxWaitDuration = 5.seconds
))Content copied to clipboard
With fallback:
val result = bulkhead.executeOrFallback(
fallback = { cachedResponse() }
) {
callExternalService()
}Content copied to clipboard
DSL builder:
val bulkhead = bulkhead {
maxConcurrentCalls = 5
maxWaitingCalls = 10
maxWaitDuration = 3.seconds
}Content copied to clipboard
With listener:
bulkhead.addListener(object : BulkheadListener {
override fun onCallRejected() {
metrics.incrementRejected()
}
})Content copied to clipboard
Statistics:
val stats = bulkhead.statistics()
println("Total: ${stats.totalCalls}, Rejected: ${stats.rejectedCalls}")
println("Utilization: ${stats.utilizationRate * 100}%")Content copied to clipboard
Functions
Link copied to clipboard
Returns the current number of active calls.
Link copied to clipboard
Adds a listener for bulkhead events.
Link copied to clipboard
Executes an operation with a fallback if the bulkhead is full or times out.
Link copied to clipboard
Resets all statistics counters to zero.
Link copied to clipboard
Returns a snapshot of the current bulkhead statistics.
Link copied to clipboard
Tries to execute an operation, returning null if the bulkhead is full or times out.