Skip to content

Commit 84e13a0

Browse files
committed
Reorder
1 parent 1141fa6 commit 84e13a0

9 files changed

Lines changed: 45 additions & 46 deletions

File tree

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ bizkit_circuit_breaker:
8686
# Service ID of the PSR-6 cache pool used to store circuit breaker state.
8787
storage: cache.circuit_breaker
8888
failure_threshold: 5
89-
success_threshold: 1
9089
failure_time_window: 20
9190
open_timeout: 30
91+
success_threshold: 1
9292
half_open_timeout: 20
9393
half_open_max_concurrent_attempts: 1
9494
half_open_attempt_timeout: 5
@@ -107,9 +107,9 @@ bizkit_circuit_breaker:
107107
# Service ID of the PSR-6 cache pool used to store circuit breaker state.
108108
storage: cache.circuit_breaker
109109
failure_threshold: 3
110-
success_threshold: 1
111110
failure_time_window: 20
112111
open_timeout: 60
112+
success_threshold: 1
113113
half_open_timeout: 20
114114
half_open_max_concurrent_attempts: 1
115115
half_open_attempt_timeout: 5

‎config/definition.php‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@
4747
->defaultValue(5)
4848
->min(1)
4949
->end()
50-
->integerNode('success_threshold')
51-
->info('Number of successful half-open attempts required before the circuit closes.')
52-
->defaultValue(1)
53-
->min(1)
54-
->end()
5550
->integerNode('failure_time_window')
5651
->info('Number of seconds during which closed-state failures are counted.')
5752
->defaultValue(20)
@@ -62,6 +57,11 @@
6257
->defaultValue(30)
6358
->min(1)
6459
->end()
60+
->integerNode('success_threshold')
61+
->info('Number of successful half-open attempts required before the circuit closes.')
62+
->defaultValue(1)
63+
->min(1)
64+
->end()
6565
->integerNode('half_open_timeout')
6666
->info('Number of seconds a half-open circuit waits for attempt results before closing.')
6767
->defaultValue(20)
@@ -99,11 +99,6 @@
9999
->defaultValue(5)
100100
->min(1)
101101
->end()
102-
->integerNode('success_threshold')
103-
->info('Number of successful half-open attempts required before the circuit closes.')
104-
->defaultValue(1)
105-
->min(1)
106-
->end()
107102
->integerNode('failure_time_window')
108103
->info('Number of seconds during which closed-state failures are counted.')
109104
->defaultValue(20)
@@ -114,6 +109,11 @@
114109
->defaultValue(30)
115110
->min(1)
116111
->end()
112+
->integerNode('success_threshold')
113+
->info('Number of successful half-open attempts required before the circuit closes.')
114+
->defaultValue(1)
115+
->min(1)
116+
->end()
117117
->integerNode('half_open_timeout')
118118
->info('Number of seconds a half-open circuit waits for attempt results before closing.')
119119
->defaultValue(20)

‎src/CircuitBreaker/CircuitBreaker.php‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public function tryAcquireAttempt(string $serviceName): Attempt
4343
}
4444

4545
$now = $this->timestamp();
46-
do {
47-
$attemptToken = self::createAttemptToken();
48-
} while (isset($record->attempts[$attemptToken]));
46+
$attemptToken = self::createAttemptToken();
4947

5048
$attemptExpiresAt = $now + $this->config->halfOpenAttemptTimeout;
5149
if (null !== $record->expiresAt) {
@@ -138,6 +136,7 @@ public function recordSuccess(string $serviceName, Attempt $attempt): void
138136
}
139137

140138
$record = $record->withoutAttempt($attempt->getToken());
139+
141140
$this->storage->save(
142141
$serviceName,
143142
new CircuitRecord(

‎src/CircuitBreaker/Settings.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ public function __construct(
1010
/** Number of failures inside the failure time window before the circuit opens. */
1111
public readonly int $failureThreshold = 5,
1212

13-
/** Number of successful half-open attempts required before the circuit closes. */
14-
public readonly int $successThreshold = 1,
15-
1613
/** Number of seconds during which closed-state failures are counted. */
1714
public readonly int $failureTimeWindow = 20,
1815

1916
/** Number of seconds an open circuit stays open before moving to half-open. */
2017
public readonly int $openTimeout = 30,
2118

19+
/** Number of successful half-open attempts required before the circuit closes. */
20+
public readonly int $successThreshold = 1,
21+
2222
/** Number of seconds a half-open circuit waits for attempt results before closing. */
2323
public readonly int $halfOpenTimeout = 20,
2424

‎src/CircuitBreaker/Storage/CircuitRecord.php‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ public function __construct(
2222
) {
2323
}
2424

25-
/**
26-
* @return array{state: string, failure_count: int, success_count: int, failure_window_started_at: ?int, expires_at: ?int, attempts: array<string, int>}
27-
*/
28-
public function toArray(): array
29-
{
30-
return [
31-
'state' => $this->state->value,
32-
'failure_count' => $this->failureCount,
33-
'success_count' => $this->successCount,
34-
'failure_window_started_at' => $this->failureWindowStartedAt,
35-
'expires_at' => $this->expiresAt,
36-
'attempts' => $this->attempts,
37-
];
38-
}
39-
4025
/**
4126
* @return array<string, int>
4227
*/
@@ -84,6 +69,21 @@ public function withoutAttempt(?string $token): self
8469
);
8570
}
8671

72+
/**
73+
* @return array{state: string, failure_count: int, success_count: int, failure_window_started_at: ?int, expires_at: ?int, attempts: array<string, int>}
74+
*/
75+
public function toArray(): array
76+
{
77+
return [
78+
'state' => $this->state->value,
79+
'failure_count' => $this->failureCount,
80+
'success_count' => $this->successCount,
81+
'failure_window_started_at' => $this->failureWindowStartedAt,
82+
'expires_at' => $this->expiresAt,
83+
'attempts' => $this->attempts,
84+
];
85+
}
86+
8787
/**
8888
* @param array<string, mixed> $value
8989
*/

‎src/DependencyInjection/CircuitBreakerHttpClientCompilerPass.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public function process(ContainerBuilder $container): void
3030
* @var array{
3131
* storage: ?string,
3232
* failure_threshold: int,
33-
* success_threshold: int,
3433
* failure_time_window: int,
3534
* open_timeout: int,
35+
* success_threshold: int,
3636
* half_open_timeout: int,
3737
* half_open_max_concurrent_attempts: int,
3838
* half_open_attempt_timeout: int,
@@ -55,9 +55,9 @@ public function process(ContainerBuilder $container): void
5555
* @var array<string, array{
5656
* storage: ?string,
5757
* failure_threshold: int,
58-
* success_threshold: int,
5958
* failure_time_window: int,
6059
* open_timeout: int,
60+
* success_threshold: int,
6161
* half_open_timeout: int,
6262
* half_open_max_concurrent_attempts: int,
6363
* half_open_attempt_timeout: int,
@@ -86,9 +86,9 @@ public function process(ContainerBuilder $container): void
8686
* @param array{
8787
* storage: string,
8888
* failure_threshold: int,
89-
* success_threshold: int,
9089
* failure_time_window: int,
9190
* open_timeout: int,
91+
* success_threshold: int,
9292
* half_open_timeout: int,
9393
* half_open_max_concurrent_attempts: int,
9494
* half_open_attempt_timeout: int,
@@ -131,9 +131,9 @@ private static function decorateClient(ContainerBuilder $container, string $clie
131131
$container->register($configId = $idPrefix.'.config', Settings::class)
132132
->setArguments([
133133
$config['failure_threshold'],
134-
$config['success_threshold'],
135134
$config['failure_time_window'],
136135
$config['open_timeout'],
136+
$config['success_threshold'],
137137
$config['half_open_timeout'],
138138
$config['half_open_max_concurrent_attempts'],
139139
$config['half_open_attempt_timeout'],

‎tests/BizkitCircuitBreakerBundleTest.php‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function testLoadsHttpClientConfiguration(): void
4343
'http_client' => [
4444
'storage' => 'cache.app',
4545
'failure_threshold' => 6,
46-
'success_threshold' => 2,
4746
'failure_time_window' => 40,
4847
'open_timeout' => 50,
48+
'success_threshold' => 2,
4949
'half_open_timeout' => 15,
5050
'half_open_max_concurrent_attempts' => 2,
5151
'half_open_attempt_timeout' => 7,
@@ -57,9 +57,9 @@ public function testLoadsHttpClientConfiguration(): void
5757
self::assertSame([
5858
'storage' => 'cache.app',
5959
'failure_threshold' => 6,
60-
'success_threshold' => 2,
6160
'failure_time_window' => 40,
6261
'open_timeout' => 50,
62+
'success_threshold' => 2,
6363
'half_open_timeout' => 15,
6464
'half_open_max_concurrent_attempts' => 2,
6565
'half_open_attempt_timeout' => 7,
@@ -89,9 +89,9 @@ public function testLoadsScopedHttpClientConfiguration(): void
8989
'client1' => [
9090
'storage' => 'cache.client1',
9191
'failure_threshold' => 2,
92-
'success_threshold' => 1,
9392
'failure_time_window' => 20,
9493
'open_timeout' => 30,
94+
'success_threshold' => 1,
9595
'half_open_timeout' => 20,
9696
'half_open_max_concurrent_attempts' => 1,
9797
'half_open_attempt_timeout' => 5,

‎tests/CircuitBreaker/CircuitBreakerTest.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ public function testHalfOpenReleasesAttemptAfterSuccessWhenSuccessThresholdIsNot
101101
$clock = new MockClock();
102102
$circuitBreaker = new CircuitBreaker(new InMemoryStorage(), new Settings(
103103
failureThreshold: 1,
104-
successThreshold: 2,
105104
openTimeout: 10,
105+
successThreshold: 2,
106106
halfOpenMaxConcurrentAttempts: 1,
107107
), $clock);
108108

@@ -125,8 +125,8 @@ public function testHalfOpenSuccessReleasesOnlyMatchingAttempt(): void
125125
$storage = new InMemoryStorage();
126126
$circuitBreaker = new CircuitBreaker($storage, new Settings(
127127
failureThreshold: 1,
128-
successThreshold: 3,
129128
openTimeout: 10,
129+
successThreshold: 3,
130130
halfOpenMaxConcurrentAttempts: 2,
131131
), $clock);
132132

@@ -151,8 +151,8 @@ public function testHalfOpenRequiresSuccessThresholdBeforeClosing(): void
151151
$clock = new MockClock();
152152
$circuitBreaker = new CircuitBreaker(new InMemoryStorage(), new Settings(
153153
failureThreshold: 1,
154-
successThreshold: 2,
155154
openTimeout: 10,
155+
successThreshold: 2,
156156
halfOpenTimeout: 10,
157157
), $clock);
158158

@@ -174,8 +174,8 @@ public function testBlockedAttemptDoesNotRecordSuccess(): void
174174
$clock = new MockClock();
175175
$circuitBreaker = new CircuitBreaker(new InMemoryStorage(), new Settings(
176176
failureThreshold: 1,
177-
successThreshold: 1,
178177
openTimeout: 10,
178+
successThreshold: 1,
179179
), $clock);
180180

181181
$circuitBreaker->recordFailure('api');

‎tests/DependencyInjection/CircuitBreakerHttpClientCompilerPassTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testDecoratesScopedHttpClient(): void
9494
self::assertSame('cache.app', (string) $storage->getArgument(0));
9595

9696
$config = $container->getDefinition($configId);
97-
self::assertSame([2, 1, 20, 30, 17, 1, 5], $config->getArguments());
97+
self::assertSame([2, 20, 30, 1, 17, 1, 5], $config->getArguments());
9898

9999
$decorator = $container->getDefinition($decoratorId);
100100
self::assertSame('bizkit_circuit_breaker.failure_checker.default', (string) $decorator->getArgument(2));
@@ -293,9 +293,9 @@ private static function config(array $configOverrides = []): array
293293
return array_replace([
294294
'storage' => null,
295295
'failure_threshold' => 5,
296-
'success_threshold' => 1,
297296
'failure_time_window' => 20,
298297
'open_timeout' => 30,
298+
'success_threshold' => 1,
299299
'half_open_timeout' => 20,
300300
'half_open_max_concurrent_attempts' => 1,
301301
'half_open_attempt_timeout' => 5,

0 commit comments

Comments
 (0)