Module: BreakerMachines::HedgedAsyncSupport

Defined in:
lib/breaker_machines/hedged_async_support.rb

Overview

AsyncSupport for HedgedExecution

Instance Method Summary collapse

Instance Method Details

#execute_hedged_with_async(callables, delay_ms) ⇒ Object

Execute hedged requests with configurable delay between attempts

Parameters:

  • callables (Array<Proc>)

    Array of callables to execute

  • delay_ms (Integer)

    Milliseconds to wait before starting hedged requests

Returns:

  • (Object)

    Result from the first successful callable

Raises:

  • (StandardError)

    If all callables fail



21
22
23
# File 'lib/breaker_machines/hedged_async_support.rb', line 21

def execute_hedged_with_async(callables, delay_ms)
  race_tasks(callables, delay_ms: delay_ms)
end

#execute_parallel_fallbacks_async(fallbacks) ⇒ Object

Execute parallel fallbacks without delay

Parameters:

  • fallbacks (Array<Proc,Object>)

    Array of fallback values or callables

Returns:

  • (Object)

    Result from the first successful fallback

Raises:

  • (StandardError)

    If all fallbacks fail



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/breaker_machines/hedged_async_support.rb', line 29

def execute_parallel_fallbacks_async(fallbacks)
  # Normalize fallbacks to callables
  callables = fallbacks.map do |fallback|
    case fallback
    when Proc
      # Handle procs with different arities
      -> { fallback.arity == 1 ? fallback.call(nil) : fallback.call }
    else
      # Wrap static values in callables
      -> { fallback }
    end
  end

  race_tasks(callables, delay_ms: 0)
end