DefaultBulkhead

class DefaultBulkhead(config: BulkheadConfig, onRejected: (reason: String) -> Unit? = null) : Bulkhead

A semaphore-based implementation of the Bulkhead pattern.

This implementation controls the number of concurrent executions and the number of calls waiting to be executed. It uses a Semaphore to limit the number of parallel executions to [BulkheadConfig.maxConcurrentCalls].

When all permits of the semaphore are acquired, incoming calls are placed in a waiting queue. The size of this queue is limited by [BulkheadConfig.maxWaitingCalls]. If the queue is also full, any new calls will be rejected immediately with a BulkheadFullException.

A call waiting in the queue will attempt to acquire a permit from the semaphore. If a [BulkheadConfig.timeout] is configured, the call will wait for the specified duration. If it fails to acquire a permit within this time, it is rejected.

Parameters

config

The BulkheadConfig used to configure this instance.

onRejected

Optional callback invoked synchronously when a call is rejected before throwing BulkheadFullException. Receives a human-readable reason string. Used for telemetry (e.g. emitting com.santimattius.resilient.telemetry.ResilientEvent.BulkheadRejected) regardless of whether a Fallback policy is configured.

Constructors

Link copied to clipboard
constructor(config: BulkheadConfig, onRejected: (reason: String) -> Unit? = null)

Functions

Link copied to clipboard
open suspend override fun <T> execute(block: suspend () -> T): T

Acquires a permit (or queues), executes block, then releases the permit.

Link copied to clipboard
open override fun snapshot(): BulkheadSnapshot

Returns a point-in-time snapshot of bulkhead usage for health/readiness and metrics. Implementations that support it return a BulkheadSnapshot; others may return null.