retry

Retry and circuit breaker utilities with exponential backoff and state management.

v1.0.0Application & logicbundledimport "retry";
Overview Installation Documentation Examples

Overview

Retry and circuit breaker utilities for HolyPHP with exponential backoff, retry predicates, and closed/open/half-open circuit state management.

bundled — works offline backoff circuit breaker resilience

Quick start

import "retry";

// Retry an unreliable operation up to 3 times
$result = retry_run(function(int $attempt) {
    if ($attempt < 3) {
        throw new Exception("transient network error");
    }
    return "success";
}, 3);

// Compute exponential backoff delays (in milliseconds)
$delay1 = retry_backoff_delay(1, 100, 2.0, 5000); // 100
$delay2 = retry_backoff_delay(2, 100, 2.0, 5000); // 200
$delay3 = retry_backoff_delay(3, 100, 2.0, 5000); // 400

// Protect a downstream call with a CircuitBreaker
$cb = new CircuitBreaker(3, 30, 1);
$data = $cb->call(function() {
    return "service response";
});

Installation

In a project

Inside a project (a folder with hphp.yaml, see Projects & packages):

hphp install retry

This adds the library to hphp.yaml, installs it into .hphp/packages/retry/ and pins the exact version in hphp.lock:

require:
  retry: "^1.0"

Use it

import "retry";

retry is bundled with the compiler, so import "retry"; also works in a single file without installing anything, and hphp install works offline.

Outside a project

hphp install retry          # for your user, shared by all scripts
hphp create myapp                 # or start a project, then: hphp install retry
Packageretry
Version1.0.0 (follows the compiler)
Entry fileretry.hphp
Sourcepackages/lib/retry ↗

Documentation

API Reference

retry_backoff_delay(int $attempt, int $baseMs = 100, float $multiplier = 2.0, int $maxMs = 30000): int

Calculates the exponential backoff delay in milliseconds for a given 1-based attempt number, clamped to $maxMs.

retry_run($fn, int $maxAttempts = 3, $shouldRetry = null, $onRetry = null)

Executes $fn($attempt) up to $maxAttempts times until it succeeds.

class CircuitBreaker

Implements a three-state ("closed", "open", "half-open") circuit breaker.

Constructor

new CircuitBreaker(int $failureThreshold = 5, int $resetTimeoutSec = 30, int $halfOpenMaxSuccesses = 1, $clock = null)

Methods

Error Handling

All validation and runtime errors throw Exception with clear, prefixed messages:

Examples

01Quick start

Bundled with the compiler — import "retry"; works offline in any file, or add it to a project with hphp install retry.

import "retry";

// Retry an unreliable operation up to 3 times
$result = retry_run(function(int $attempt) {
    if ($attempt < 3) {
        throw new Exception("transient network error");
    }
    return "success";
}, 3);

// Compute exponential backoff delays (in milliseconds)
$delay1 = retry_backoff_delay(1, 100, 2.0, 5000); // 100
$delay2 = retry_backoff_delay(2, 100, 2.0, 5000); // 200
$delay3 = retry_backoff_delay(3, 100, 2.0, 5000); // 400

// Protect a downstream call with a CircuitBreaker
$cb = new CircuitBreaker(3, 30, 1);
$data = $cb->call(function() {
    return "service response";
});
Try it
hphp install retry
hphp run main.hphp

Covered by the official test suite in tests/positive/lib_retry.hphp.