resilience4u/r4contracts

Shared contracts and support utilities for the Resilience4u ecosystem.

Installs: 13

Dependents: 2

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/resilience4u/r4contracts

v1.0.0 2025-11-01 00:53 UTC

This package is auto-updated.

Last update: 2025-11-01 14:15:45 UTC


README

Shared contracts and support utilities for the Resilience4u ecosystem.

R4Contracts defines the common interfaces, types, and lightweight helpers used across all Resilience4u libraries — such as R4PHP, R4Go, and R4PolicyEngine.

It provides the shared foundation for building consistent, interoperable resilience libraries — without introducing heavy dependencies.

ðŸ“Ķ Installation

composer require resilience4u/r4contracts

ðŸ§ą Structure

src/
├── Contracts/
│   ├── Policy.php
│   ├── Executable.php
│   ├── StorageAdapter.php
│   └── TelemetryAdapter.php
└── Support/
    ├── Time.php
    └── Env.php

🚀 Purpose

Area Description
Contracts Define core interfaces that describe resilience primitives (CircuitBreaker, Retry, etc.)
Support Provide minimal helper utilities (Time, Env) shared across multiple libraries
Telemetry Offer a contract (TelemetryAdapter) for optional distributed tracing and metrics — without coupling to OpenTelemetry or any exporter

🧠 Design Principles

  • ðŸŠķ Zero dependencies — pure PHP, no vendor lock-in
  • 🔄 Bidirectional reusability — can be used by R4PHP, R4Go, R4JS, or any language bridge
  • ðŸ§Đ Stable contracts — consistent method signatures across the ecosystem
  • ⚙ïļ Opt-in telemetry — instrumentation only when explicitly enabled by environment or adapter
  • ðŸ’Ą Extensible by design — supports third-party exporters, storage adapters, and policies

ðŸ§Đ Key Interfaces

Contracts\Policy

Base interface for all resilience patterns:

interface Policy
{
    public function name(): string;
    public function execute(Executable $op): mixed;
}

Contracts\Executable

Encapsulates an operation that can be retried, rate-limited, or executed under a circuit breaker:

interface Executable
{
    public function __invoke(): mixed;
}

Contracts\StorageAdapter

Abstracts how policies persist transient state (in-memory, APCu, Redis, etc.):

interface StorageAdapter
{
    public function get(string $key, mixed $default = null): mixed;
    public function set(string $key, mixed $value, int $ttl = 0): void;
    public function delete(string $key): void;
}

Contracts\TelemetryAdapter

Optional bridge for distributed tracing and metrics:

interface TelemetryAdapter
{
    public function record(string $policy, string $event, array $context = []): void;
}

🧰 Support Utilities

Support\Time

Lightweight microtime helper:

Time::nowMs(); // current timestamp in milliseconds
Time::elapsedMs($start); // measure elapsed time

Support\Env

Simple environment utility:

Env::bool('R4PHP_TELEMETRY_ENABLED', false); // returns true/false
Env::int('R4PHP_MAX_RETRIES', 3);

🧭 Ecosystem

Library Role
R4Contracts Shared contracts & utilities
R4PHP Resilience primitives for PHP (retry, circuit breaker, etc.)
R4PolicyEngine Policy orchestration & telemetry engine
R4Go Go runtime equivalent of R4PHP
Faceless Logger LGPD-first log anonymization pipeline

🧎 Example Integration

use Resilience4u\R4Contracts\Contracts\TelemetryAdapter;

class CustomTelemetry implements TelemetryAdapter
{
    public function record(string $policy, string $event, array $context = []): void
    {
        printf("[telemetry] %s - %s (%s)\n", $policy, $event, json_encode($context));
    }
}

📜 License

MIT ÂĐ Resilience4u

💎 Community

Join our discussions on Slack:
Join Slack