-
Notifications
You must be signed in to change notification settings - Fork 2
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
Introduce Backoff
implementation with exponential strategy
#50
base: main
Are you sure you want to change the base?
Conversation
b8d82b5
to
39be826
Compare
e66eec5
to
5576ae0
Compare
protected $retries; | ||
|
||
/** @var ?int The previous used retry wait time */ | ||
protected $previousWaitTime; |
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.
I would not maintain state and just do $min << $attempt
as in our go code.
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.
He didn't do that on my request.
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.
Please justify.
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.
Unnecessarily complex and difficult to understand for no apparent advantage
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.
php > $min = 100;
php > var_dump($min*2, $min << 1);
php shell code:1:
int(200)
php shell code:1:
int(200)
php > var_dump($min*2*2, $min << 2);
php shell code:1:
int(400)
php shell code:1:
int(400)
php > var_dump($min*2*2*2, $min << 3);
php shell code:1:
int(800)
php shell code:1:
int(800)
...
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.
Unnecessarily complex and difficult to understand for no apparent advantage
You can't be serious. If this is too complex (2 ** $attempt * $min
), I wonder how we can build software.
The obvious advantage is that no state is introduced without reason, which is then also changed in a getter.
The ipl should be an example of good coding practice. The current implementation is not.
The seemingly not so obvious advantage is that this does not fall apart when running asynchronously. Sure, PHP isn't ready to support everything concurrently yet, but PHP isn't the only programming language and there are fibers, async I/O (ReactPHP and the like) that already make this possible. When state like this one is introduced, concurrency should also be thought of. Even in PHP.
Also, getWaitTime()
claims to return a proper wait time in the first line and then explains in the description that it actually does not. Calling getWaitTime(n)
yields wrong results without having called getWaitTime(0..n-1)
first.
Plus, when fixed, the actual backoff implementation would be properly testable.
7ec528f
to
4230092
Compare
4230092
to
81f3d67
Compare
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.
Why the effort to make it desperately different from our Go implementation, which just works?
protected $retries; | ||
|
||
/** @var ?int The previous used retry wait time */ | ||
protected $previousWaitTime; |
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.
Unnecessarily complex and difficult to understand for no apparent advantage
You can't be serious. If this is too complex (2 ** $attempt * $min
), I wonder how we can build software.
The obvious advantage is that no state is introduced without reason, which is then also changed in a getter.
The ipl should be an example of good coding practice. The current implementation is not.
The seemingly not so obvious advantage is that this does not fall apart when running asynchronously. Sure, PHP isn't ready to support everything concurrently yet, but PHP isn't the only programming language and there are fibers, async I/O (ReactPHP and the like) that already make this possible. When state like this one is introduced, concurrency should also be thought of. Even in PHP.
Also, getWaitTime()
claims to return a proper wait time in the first line and then explains in the description that it actually does not. Calling getWaitTime(n)
yields wrong results without having called getWaitTime(0..n-1)
first.
Plus, when fixed, the actual backoff implementation would be properly testable.
public function setMin(int $min): self | ||
{ | ||
if ($min <= 0) { | ||
$min = 100; // Default minimum wait time 100 ms |
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.
Please introduce consts for the defaults.
$this->assertNotSame(10, $backoff->setRetries(5)->getRetries()); | ||
} | ||
|
||
public function testGetWaitTime() |
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.
Should also test the waiting time for attempt n
, where 0..n-1
has not yet been called.
try { | ||
return $callback($previousErr); | ||
} catch (Exception $err) { | ||
if ($attempt >= $this->getRetries() || $err === $previousErr) { |
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.
Why is the logic for deciding whether something is retryable not part of this code, but must be executed in each individual callback? With the current implementation, the decision whether something is retryable is made after sleeping (the next time the callback is called), and the first attempt cannot stop the retryable logic (please add a test). And requires duplicate code in each individual callback.
No description provided.