Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/visible/cruel/llms.txt

Use this file to discover all available pages before exploring further.

Function Signature

function createBulkhead<T extends AnyFn>(
  fn: T,
  options: BulkheadOptions
): T
Wraps a function with bulkhead pattern to limit concurrent executions. Prevents resource exhaustion by queueing excess calls.

Options

maxConcurrent
number
required
Maximum number of concurrent function executions allowed.
maxQueue
number
Maximum number of calls to queue when at max concurrency. If not set, the queue is unbounded.
onReject
() => void
Callback invoked when a call is rejected because the queue is full.

Return Type

Returns the wrapped function with concurrency limiting.

Types

interface BulkheadOptions {
  maxConcurrent: number
  maxQueue?: number
  onReject?: () => void
}

Example

import { createBulkhead } from 'cruel'

const limitedFetch = createBulkhead(
  async (url: string) => {
    const response = await fetch(url)
    return response.json()
  },
  {
    maxConcurrent: 5,
    maxQueue: 10,
    onReject: () => {
      console.warn('Too many requests queued')
    }
  }
)

// Only 5 requests will run concurrently, rest will queue
const promises = urls.map(url => limitedFetch(url))
const results = await Promise.all(promises)