Modules@robinpath/retry
retry

@robinpath/retry

0.1.1Node.jsPublic

Retry with exponential backoff and circuit breaker for RobinPath

Retry

Retry with exponential backoff and circuit breaker patterns for resilient automation workflows

Package: @robinpath/retry | Category: Infrastructure | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the retry module when you need to:

  • Execute a function with automatic retry and exponential backoff -- Use retry.execute to perform this operation
  • Calculate the delay for a given retry attempt using exponential backoff -- Use retry.withBackoff to perform this operation
  • Check if an HTTP status code is retryable (408, 429, 500, 502, 503, 504) -- Use retry.isRetryable to perform this operation
  • Wait for a specified number of milliseconds -- Use retry.delay to perform this operation
  • Preview the delay schedule for a series of retry attempts -- Use retry.attempts to perform this operation

Quick Reference

FunctionDescriptionReturns
executeExecute a function with automatic retry and exponential backoffThe result of the function if it succeeds within the retry limit
withBackoffCalculate the delay for a given retry attempt using exponential backoffThe delay in milliseconds for the given attempt
isRetryableCheck if an HTTP status code is retryable (408, 429, 500, 502, 503, 504)True if the status code is typically retryable
delayWait for a specified number of millisecondsTrue when the delay completes
attemptsPreview the delay schedule for a series of retry attemptsArray of {attempt, delay, totalWait} objects
createBreakerCreate a named circuit breaker with a failure threshold and reset timeoutThe circuit breaker configuration
breakerStateGet the current state of a named circuit breakerObject with state (closed/open/half-open), failures, threshold
breakerRecordRecord a success or failure in a circuit breakerUpdated circuit breaker state
breakerAllowCheck if a circuit breaker allows requests throughTrue if requests are allowed, false if circuit is open
breakerResetReset a circuit breaker to closed stateTrue if reset successfully

Functions

execute

Execute a function with automatic retry and exponential backoff

Module: retry | Returns: any -- The result of the function if it succeeds within the retry limit

retry.execute $myFunction {"maxAttempts": 5, "initialDelay": 2000}
ParameterTypeRequiredDescription
fnstringYesThe async function to execute
optionsobjectNoRetry options: maxAttempts, initialDelay, maxDelay, backoffFactor, retryOn, jitter

withBackoff

Calculate the delay for a given retry attempt using exponential backoff

Module: retry | Returns: number -- The delay in milliseconds for the given attempt

retry.withBackoff 3 1000 2 30000
ParameterTypeRequiredDescription
attemptnumberYesThe current attempt number (0-based)
initialDelaynumberNoBase delay in ms (default 1000)
factornumberNoBackoff multiplier (default 2)
maxDelaynumberNoMaximum delay in ms (default 30000)
jitterbooleanNoAdd random jitter (default true)

isRetryable

Check if an HTTP status code is retryable (408, 429, 500, 502, 503, 504)

Module: retry | Returns: boolean -- True if the status code is typically retryable

retry.isRetryable 503
ParameterTypeRequiredDescription
statusCodenumberYesThe HTTP status code to check

delay

Wait for a specified number of milliseconds

Module: retry | Returns: boolean -- True when the delay completes

retry.delay 2000
ParameterTypeRequiredDescription
msnumberYesMilliseconds to wait

attempts

Preview the delay schedule for a series of retry attempts

Module: retry | Returns: array -- Array of {attempt, delay, totalWait} objects

retry.attempts 5 1000 2
ParameterTypeRequiredDescription
maxAttemptsnumberNoNumber of attempts (default 3)
initialDelaynumberNoBase delay in ms (default 1000)
factornumberNoBackoff multiplier (default 2)

createBreaker

Create a named circuit breaker with a failure threshold and reset timeout

Module: retry | Returns: object -- The circuit breaker configuration

retry.createBreaker "api-service" 3 30000
ParameterTypeRequiredDescription
namestringYesCircuit breaker name
thresholdnumberNoNumber of failures before opening (default 5)
resetTimeoutnumberNoTime in ms before transitioning to half-open (default 60000)

breakerState

Get the current state of a named circuit breaker

Module: retry | Returns: object -- Object with state (closed/open/half-open), failures, threshold

retry.breakerState "api-service"
ParameterTypeRequiredDescription
namestringYesCircuit breaker name

breakerRecord

Record a success or failure in a circuit breaker

Module: retry | Returns: object -- Updated circuit breaker state

retry.breakerRecord "api-service" false
ParameterTypeRequiredDescription
namestringYesCircuit breaker name
successbooleanYesTrue for success, false or 'failure' for failure

breakerAllow

Check if a circuit breaker allows requests through

Module: retry | Returns: boolean -- True if requests are allowed, false if circuit is open

retry.breakerAllow "api-service"
ParameterTypeRequiredDescription
namestringYesCircuit breaker name

breakerReset

Reset a circuit breaker to closed state

Module: retry | Returns: boolean -- True if reset successfully

retry.breakerReset "api-service"
ParameterTypeRequiredDescription
namestringYesCircuit breaker name

Error Handling

All functions throw on failure. Common errors:

ErrorCause
First argument must be a callable functionCheck the error message for details
Circuit breaker "..." not found. Create it first.Check the error message for details
@desc "Execute and validate result"
do
  set $result as retry.execute $myFunction {"maxAttempts": 5, "initialDelay": 2000}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. Create a new item with createBreaker

Create a new resource and capture the result.

set $result as retry.createBreaker "api-service" 3 30000
print "Created: " + $result

2. Multi-step Retry workflow

Chain multiple retry operations together.

@desc "Execute, with backoff, and more"
do
  set $r_execute as retry.execute $myFunction {"maxAttempts": 5, "initialDelay": 2000}
  set $r_withBackoff as retry.withBackoff 3 1000 2 30000
  set $r_isRetryable as retry.isRetryable 503
  print "All operations complete"
enddo

3. Safe execute with validation

Check results before proceeding.

@desc "Execute and validate result"
do
  set $result as retry.execute $myFunction {"maxAttempts": 5, "initialDelay": 2000}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/retry

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.8 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Category

devops