Skip to content

Commit a050035

Browse files
author
Aleksandr Denisyuk
authored
Release 4.0.0
2 parents 3d2070b + 790df2f commit a050035

File tree

87 files changed

+1577
-1217
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+1577
-1217
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
/README.md export-ignore
99
/build/ export-ignore
1010
/composer.lock export-ignore
11-
/docs/ export-ignore
1211
/phpunit.xml.dist export-ignore
1312
/psalm-baseline.xml export-ignore
1413
/psalm.xml export-ignore

.github/workflows/ci.yml

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ on:
44
push:
55
paths-ignore:
66
- 'build/**'
7-
- 'docs/**'
87
- '.editorconfig'
98
- '.gitattributes'
109
- 'LICENSE'
1110
- 'README.md'
1211
pull_request:
1312
paths-ignore:
1413
- 'build/**'
15-
- 'docs/**'
1614
- '.editorconfig'
1715
- '.gitattributes'
1816
- 'LICENSE'
@@ -22,68 +20,55 @@ jobs:
2220
parallel-lint:
2321
name: 'ParallelLint'
2422
runs-on: 'ubuntu-latest'
25-
2623
steps:
2724
- name: Checkout repository
2825
uses: actions/checkout@v3
29-
3026
- name: Setup PHP
3127
uses: shivammathur/setup-php@v2
3228
with:
33-
php-version: '8.1'
29+
php-version: '8.2'
3430
coverage: none
35-
3631
- name: Install dependencies
3732
uses: ramsey/composer-install@v2
38-
3933
- name: Run ParallelLint
4034
run: composer parallel-lint -- --no-progress --ignore-fails
4135

4236
psalm:
4337
name: 'Psalm'
4438
runs-on: 'ubuntu-latest'
45-
4639
steps:
4740
- name: Checkout repository
4841
uses: actions/checkout@v3
49-
5042
- name: Setup PHP
5143
uses: shivammathur/setup-php@v2
5244
with:
53-
php-version: '8.1'
45+
php-version: '8.2'
5446
coverage: none
55-
5647
- name: Install dependencies
5748
uses: ramsey/composer-install@v2
58-
5949
- name: Run Psalm
60-
run: composer psalm -- --no-progress --no-cache --output-format=github
50+
run: composer psalm -- --show-info=false --no-progress --no-suggestions --no-cache
6151

6252
php-cs-fixer:
6353
name: 'PHPCsFixer'
6454
runs-on: 'ubuntu-latest'
65-
6655
steps:
6756
- name: Checkout repository
6857
uses: actions/checkout@v3
69-
7058
- name: Setup PHP
7159
uses: shivammathur/setup-php@v2
7260
with:
73-
php-version: '8.1'
61+
php-version: '8.2'
7462
coverage: none
75-
7663
- name: Install dependencies
7764
uses: ramsey/composer-install@v2
78-
7965
- name: Run PHPCsFixer
8066
run: composer php-cs-fixer:diff -- --no-interaction --using-cache=no
8167

8268
phpunit:
8369
name: 'PHPUnit'
8470
needs: ['parallel-lint', 'psalm', 'php-cs-fixer']
8571
runs-on: ${{ matrix.operating-system }}
86-
8772
strategy:
8873
fail-fast: false
8974
matrix:
@@ -92,24 +77,21 @@ jobs:
9277
- 'windows-latest'
9378
php-version:
9479
- '8.1'
80+
- '8.2'
9581
composer-dependency:
9682
- 'lowest'
9783
- 'highest'
98-
9984
steps:
10085
- name: Checkout repository
10186
uses: actions/checkout@v3
102-
10387
- name: Setup PHP
10488
uses: shivammathur/setup-php@v2
10589
with:
10690
php-version: ${{ matrix.php-version }}
10791
coverage: none
108-
10992
- name: Install dependencies
11093
uses: ramsey/composer-install@v2
11194
with:
11295
dependency-versions: ${{ matrix.composer-dependency }}
113-
11496
- name: Run PHPUnit
11597
run: composer phpunit -- --no-interaction --do-not-cache-result

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM php:8.2-cli
2+
3+
RUN \
4+
apt-get update ; \
5+
apt-get install -y unzip ; \
6+
pecl install pcov ; \
7+
docker-php-ext-enable pcov ;
8+
9+
COPY --from=composer:2.4 /usr/bin/composer /usr/local/bin/composer
10+
11+
WORKDIR /usr/local/packages/backoff/

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright (c) 2022 Orangesoft
1+
Copyright (c) 2021 Orangesoft
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy
44
of this software and associated documentation files (the "Software"), to deal

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
init:
2+
docker build -t backoff:8.2 ./
3+
4+
exec:
5+
docker run --name backoff --rm --interactive --tty --volume ${PWD}:/usr/local/packages/backoff/ backoff:8.2 /bin/bash

README.md

Lines changed: 72 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,36 @@ This package requires PHP 8.1 or later.
2020

2121
## Quick usage
2222

23-
Configure BackOff and ExceptionClassifier to retry your business logic when an exception will be thrown:
23+
Configure `Orangesoft\BackOff\Retry\BackOffRetry::class`, any of back-off classes, and `Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier::class` to retry a business logic when an exception is thrown:
2424

2525
```php
2626
<?php
2727

2828
use Orangesoft\BackOff\ExponentialBackOff;
29-
use Orangesoft\BackOff\Duration\Seconds;
29+
use Orangesoft\BackOff\Duration\Microseconds;
3030
use Orangesoft\BackOff\Retry\ExceptionClassifier\ExceptionClassifier;
31-
use Orangesoft\BackOff\Retry\Retry;
31+
use Orangesoft\BackOff\Retry\BackOffRetry;
3232

33-
$backOff = new ExponentialBackOff(
33+
$backOffRetry = new BackOffRetry(
3434
maxAttempts: 3,
35-
baseTime: new Seconds(1),
36-
capTime: new Seconds(60),
35+
baseTime: new Microseconds(1_000),
36+
capTime: new Microseconds(10_000),
37+
backOff: new ExponentialBackOff(
38+
multiplier: 2.0,
39+
),
40+
exceptionClassifier: new ExceptionClassifier(
41+
classNames: [
42+
\Exception::class,
43+
],
44+
),
3745
);
38-
39-
$exceptionClassifier = new ExceptionClassifier([
40-
\RuntimeException::class,
41-
]);
42-
43-
$retry = new Retry($backOff, $exceptionClassifier);
4446
```
4547

46-
Put the business logic in a callback function and call it:
48+
Use the `Orangesoft\BackOff\Retry\BackOffRetry::call(callable $callback): mixed` method to wrap the business logic and call it with retry functionality:
4749

4850
```php
49-
$retry->call(function (): int {
51+
/** @var int $result */
52+
$result = $backOffRetry->call(static function (): int {
5053
$random = mt_rand(5, 10);
5154

5255
if (0 === $random % 2) {
@@ -57,14 +60,61 @@ $retry->call(function (): int {
5760
});
5861
```
5962

60-
After the exception is thrown call will be retried with a back-off time until max attempts has been reached.
63+
The following back-off strategies are available:
64+
65+
- [Orangesoft\BackOff\CallbackBackOff](./src/CallbackBackOff.php)
66+
- [Orangesoft\BackOff\DecorrelatedJitterBackOff](./src/DecorrelatedJitterBackOff.php)
67+
- [Orangesoft\BackOff\ExponentialBackOff](./src/ExponentialBackOff.php)
68+
- [Orangesoft\BackOff\FibonacciBackOff](./src/FibonacciBackOff.php)
69+
- [Orangesoft\BackOff\LinearBackOff](./src/LinearBackOff.php)
70+
- [Orangesoft\BackOff\PermanentBackOff](./src/PermanentBackOff.php)
71+
72+
## Enable Jitter
73+
74+
Pass the implementation of `Orangesoft\BackOff\Jitter\JitterInterface::class` to the back-off class and jitter will be enabled:
75+
76+
```php
77+
<?php
78+
79+
use Orangesoft\BackOff\ExponentialBackOff;
80+
use Orangesoft\BackOff\Duration\Microseconds;
81+
use Orangesoft\BackOff\Jitter\EqualJitter;
82+
83+
$exponentialBackOff = new ExponentialBackOff(
84+
multiplier: 2.0,
85+
jitter: new EqualJitter(),
86+
);
87+
88+
$exponentialBackOff->backOff(
89+
attempt: 1,
90+
baseTime: new Microseconds(1_000),
91+
capTime: new Microseconds(512_000),
92+
);
93+
```
94+
95+
Below you can see the time intervals in microseconds for exponential back-off with a multiplier of 2.0 and equal jitter, where the base time is 1000 μs and the cap time is 512000 μs:
96+
97+
```text
98+
+---------+---------------------------+--------------------+
99+
| attempt | exponential back-off (μs) | equal jitter (μs) |
100+
+---------+---------------------------+--------------------+
101+
| 1 | 1_000 | [0, 1_000] |
102+
| 2 | 2_000 | [1_000, 2_000] |
103+
| 3 | 4_000 | [2_000, 4_000] |
104+
| 4 | 8_000 | [4_000, 8_000] |
105+
| 5 | 16_000 | [8_000, 16_000] |
106+
| 6 | 32_000 | [16_000, 32_000] |
107+
| 7 | 64_000 | [32_000, 64_000] |
108+
| 8 | 128_000 | [64_000, 128_000] |
109+
| 9 | 256_000 | [128_000, 256_000] |
110+
| 10 | 512_000 | [256_000, 512_000] |
111+
+---------+---------------------------+--------------------+
112+
```
61113

62-
## Documentation
114+
The following jitters are available:
63115

64-
- [Configure Generator](docs/index.md#configure-generator)
65-
- [Enable Jitter](docs/index.md#enable-jitter)
66-
- [Duration sleep](docs/index.md#duration-sleep)
67-
- [Use BackOff](docs/index.md#use-backoff)
68-
- [Retry exceptions](docs/index.md#retry-exceptions)
116+
- [Orangesoft\BackOff\Jitter\EqualJitter](./src/Jitter/EqualJitter.php)
117+
- [Orangesoft\BackOff\Jitter\FullJitter](./src/Jitter/FullJitter.php)
118+
- [Orangesoft\BackOff\Jitter\ScatteredJitter](./src/Jitter/ScatteredJitter.php)
69119

70-
Read more about Back-off and Jitter on [AWS Architecture Blog](https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter/).
120+
Read more about Back-Off and Jitter on [AWS Architecture Blog](https://aws.amazon.com/ru/blogs/architecture/exponential-backoff-and-jitter/).

composer.json

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
{
22
"name": "orangesoft/backoff",
3-
"description": "Back-off algorithm implementation",
3+
"description": "Back-off algorithm implementation.",
44
"keywords": [
55
"backoff",
6-
"exponential",
7-
"linear",
8-
"constant",
9-
"decorrelation",
6+
"duration",
107
"jitter",
11-
"sleeper",
12-
"retry"
8+
"retry",
9+
"sleeper"
1310
],
1411
"license": "MIT",
1512
"authors": [
@@ -24,7 +21,8 @@
2421
"sort-packages": true
2522
},
2623
"require": {
27-
"php": "^8.1"
24+
"php": "^8.1",
25+
"beberlei/assert": "^3.3"
2826
},
2927
"require-dev": {
3028
"friendsofphp/php-cs-fixer": "^3.10",
@@ -38,7 +36,10 @@
3836
"autoload": {
3937
"psr-4": {
4038
"Orangesoft\\BackOff\\": "./src/"
41-
}
39+
},
40+
"files": [
41+
"./helpers.php"
42+
]
4243
},
4344
"autoload-dev": {
4445
"psr-4": {
@@ -47,15 +48,18 @@
4748
},
4849
"scripts": {
4950
"phpunit": "./vendor/bin/phpunit --verbose --colors=always --no-coverage",
51+
"phpunit:clear-cache": "rm ./build/cache/phpunit.cache",
5052
"phpunit-coverage": "./vendor/bin/phpunit --verbose --colors=always --coverage-text",
5153
"phpunit-coverage-html": "./vendor/bin/phpunit --verbose --colors=always --coverage-html ./build/logs/phpunit-coverage/",
5254
"parallel-lint": "./vendor/bin/parallel-lint --colors ./src/ ./tests/",
5355
"php-cs-fixer:fix": "./vendor/bin/php-cs-fixer fix --verbose --ansi --show-progress=dots",
5456
"php-cs-fixer:diff": "./vendor/bin/php-cs-fixer fix --verbose --ansi --dry-run --diff",
57+
"php-cs-fixer:clear-cache": "rm ./build/cache/php-cs-fixer.cache",
5558
"psalm": "./vendor/bin/psalm --show-info=true",
56-
"psalm:set-baseline": "@psalm --set-baseline=./psalm-baseline.xml",
57-
"psalm:update-baseline": "@psalm --update-baseline",
58-
"psalm:ignore-baseline": "@psalm --ignore-baseline",
59+
"psalm:clear-cache": "rm -rf ./build/cache/psalm/",
60+
"psalm:set-baseline": "@psalm --set-baseline=./psalm-baseline.xml --no-cache",
61+
"psalm:update-baseline": "@psalm --update-baseline --no-cache",
62+
"psalm:ignore-baseline": "@psalm --ignore-baseline --no-cache",
5963
"test": [
6064
"@parallel-lint",
6165
"@psalm",

0 commit comments

Comments
 (0)