Download the PHP package zeusi/circuit-breaker without Composer

On this page you can find all versions of the php package zeusi/circuit-breaker. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package circuit-breaker

Circuit Breaker

Packagist Version PHP CI PHPStan Coverage

A circuit breaker for PHP with bucketed sliding windows and configurable half-open policies, sharing its state through APCu, Redis or Memcached.

When a service your application depends on stops answering, every call to it holds a PHP worker until it times out, and the workers pile up until the application itself stops answering. A circuit breaker notices the failures and, for a while, tells your code not to call that service: your code falls back instead, your workers stay free and the service gets time to recover.

Installation

Requires 64-bit PHP 8.1 or later, plus the extension of the storage you use: apcu, redis (phpredis) or memcached.

Usage

A circuit breaker protects one service, identified by its name. It holds no state of its own, so creating one per call, or per host discovered at runtime, costs nothing.

Registering outcomes

The circuit measures whether the service is alive. Register:

A missing outcome is only a sample less: the circuit never waits for it.

Two settings to get right

How it works

There is no background process: a transition due to time happens on the first call that finds it due, and its event is emitted then. After a long period without calls, the circuit skips the half-open state and closes: no traffic reached the service in the meantime.

getState() returns the state the next call would find, without changing anything.

How outcomes are counted

The time window slides: the failure rate always covers the last timeWindow seconds. Unlike consecutive fixed windows, a burst of failures across the boundary of two intervals is not split in half.

Configuration

Parameter Unit Limits
timeWindow seconds 10 to 3600
failureRateThreshold percentage 1 to 100
minimumRequests calls at least 1
openDuration seconds 1 to 30 days
halfOpenPolicy maximum duration from 1 second to 30 days

Durations are floats, with millisecond precision. Every parameter is required.

Half-open policies

A ramp permits a growing ratio of calls during its duration, then closes the circuit. It tests the service on real traffic and protects it from getting the full load at once:

The exponential ramp spends more time on low ratios: half way through, 10% of the calls are permitted, against about 50% for the linear one. The initial ratio is relative to your traffic: on a busy service, 1% may already be many calls.

The linear ramp is a power curve with exponent 1. A greater exponent starts more slowly, a lower one faster:

Permitted calls permit the first calls of the half-open state and close the circuit once they all reported back, or at the latest after the maximum duration:

A single successful trial closes the circuit even for a service that succeeds only now and then, and sends it the full traffic: the ramp is usually the safer choice.

A ramp can follow any curve, mapping its progress (0 to 1) to the ratio of calls permitted:

Storages

Storage Shared by Notes
ApcuStorage the processes of one PHP master (a PHP-FPM pool, a CLI command) needs apc.enable_cli=1 on the CLI
RedisStorage every server phpredis, standalone or cluster
MemcachedStorage every server the memcached extension
InMemoryStorage the current process tests and long-running processes

The shared storages accept a prefix, to separate applications sharing the same backend. Without it, two applications calling the same service name share its circuit.

APCu

APCu memory belongs to a single PHP master process, so every server, every PHP-FPM pool and every CLI command has its own circuits, opened by the failures it sees. When circuits must be shared across servers, use Redis or Memcached.

APCu has no background cleanup: an expired entry is removed when another entry is inserted in the same slot of its hash table. Since buckets are inserted continuously, the number of entries grows for a while, then stays around the live entries plus the number of slots (about four times apc.entries_hint, 4096 by default). This is expected and bounded. With many services, raise apc.entries_hint to keep lookups fast. If the rest of the application fills the cache, APCu before 5.1.25 with apc.ttl=0 clears it entirely, and every circuit restarts closed.

Redis

Memcached

The keys of a service are kept on the same server.

Events

Pass an event listener to observe the circuit, for logging, metrics or alerting:

Event When
CircuitOpened the circuit opened, from closed or again from half-open (previousState)
CircuitHalfOpened the circuit started testing the service
CircuitClosed the circuit closed
CallRejected a call was not permitted (state: open or half-open)
StorageFailed the storage failed and the call went through (exception)

CallRejected is emitted for every rejected call: count it in a metric rather than logging it. Transitions are emitted once, even with many concurrent workers. Exceptions thrown by the listener are caught and ignored, so a failing listener never breaks the call it observes.

When the storage fails

Calls are permitted, outcomes are dropped, and a StorageFailed event is emitted. The circuit does not protect the service until the storage recovers, so watch that event. getState(), meant for diagnostics, throws the storage exception instead.

Testing

Code using the circuit breaker can depend on CircuitBreakerInterface and replace it with a test double. To run a real circuit breaker in tests, use InMemoryStorage and pass a ClockInterface implementation to control time, to both the storage and the circuit breaker.

License

MIT, see LICENSE.


All versions of circuit-breaker with dependencies

PHP Build Version
Package Version
Requires php Version >=8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package zeusi/circuit-breaker contains the following files

Loading the files please wait ...