-
Notifications
You must be signed in to change notification settings - Fork 21
Add circuit breaker support to RedisPool #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests; | ||
|
|
||
| use PHPUnit\Framework\TestCase; | ||
| use Utopia\Abuse\Abuse; | ||
| use Utopia\Abuse\Adapters\TimeLimit\RedisPool; | ||
| use Utopia\CircuitBreaker\CircuitBreaker; | ||
| use Utopia\Pools\Adapter\Stack; | ||
| use Utopia\Pools\Pool; | ||
|
|
||
| class RedisPoolCircuitBreakerTest extends TestCase | ||
| { | ||
| public function testCircuitBreakerFailsOpenWhenPoolUnavailable(): void | ||
| { | ||
| $pool = new Pool(new Stack(), 'abuse-redis-unavailable', 1, function (): \Redis { | ||
| throw new \Exception('Redis unavailable'); | ||
| }); | ||
| $pool | ||
| ->setReconnectAttempts(1) | ||
| ->setRetryAttempts(1); | ||
|
|
||
| /** @var Pool<\Redis> $pool */ | ||
| $adapter = new RedisPool( | ||
| 'redis-pool-unavailable', | ||
| 1, | ||
| 60, | ||
| $pool, | ||
| new CircuitBreaker() | ||
| ); | ||
|
|
||
| $abuse = new Abuse($adapter); | ||
|
|
||
|
Comment on lines
+29
to
+33
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The test should construct the breaker with Prompt To Fix With AIThis is a comment left during a code review.
Path: tests/Abuse/RedisPoolCircuitBreakerTest.php
Line: 29-33
Comment:
**Test uses a breaker that won't open in time**
`new CircuitBreaker()` defaults to `threshold: 3`, meaning the circuit stays CLOSED for the first two failures and re-throws the exception on each. `$abuse->check()` triggers exactly one `guard()` call (via `count()`), so the pool throws once, the circuit records one failure (1 < 3), and the exception propagates through `count()` → `check()` → the test instead of returning `false`. The assertion is never reached.
The test should construct the breaker with `threshold: 1` so the circuit opens on the very first failed pool call and the fallback (`0`) is returned immediately.
How can I resolve this? If you propose a fix, please make it concise. |
||
| $this->assertFalse($abuse->check()); | ||
| $this->assertSame([], $adapter->getLogs()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the circuit is open,
guard()returnsnullwithout writing to Redis, but$this->count = ($this->count ?? 0) + 1still runs on line 101. Within the same request this inflates the cached count to 1 (matching the limit), which would make a secondcheck()call on the same adapter instance report abuse (count >= limit) even though nothing was recorded in Redis. This is a latent inconsistency: if callers reuse the same adapter for more than one check per request cycle, the second call will always report abuse when the circuit is open, regardless of actual Redis state.Prompt To Fix With AI