Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

yhabteab
Copy link
Member

No description provided.

@cla-bot cla-bot bot added the cla/signed label Sep 13, 2023
@yhabteab yhabteab added the enhancement New feature or request label Sep 13, 2023
@yhabteab yhabteab self-assigned this Sep 13, 2023
@yhabteab yhabteab requested a review from nilmerg September 13, 2023 11:23
src/ExponentialBackoff.php Show resolved Hide resolved
src/ExponentialBackoff.php Outdated Show resolved Hide resolved
@yhabteab yhabteab force-pushed the exponential-backoff branch 2 times, most recently from e66eec5 to 5576ae0 Compare September 13, 2023 12:32
@yhabteab yhabteab requested a review from nilmerg September 13, 2023 12:33
src/ExponentialBackoff.php Outdated Show resolved Hide resolved
protected $retries;

/** @var ?int The previous used retry wait time */
protected $previousWaitTime;
Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please justify.

Copy link
Member

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

Copy link
Member Author

@yhabteab yhabteab Sep 13, 2023

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)
...

Copy link
Member

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.

@yhabteab yhabteab force-pushed the exponential-backoff branch 2 times, most recently from 7ec528f to 4230092 Compare September 13, 2023 14:10
src/ExponentialBackoff.php Outdated Show resolved Hide resolved
@yhabteab yhabteab requested a review from nilmerg September 13, 2023 15:04
Copy link
Member

@lippserd lippserd left a 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;
Copy link
Member

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
Copy link
Member

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()
Copy link
Member

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) {
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla/signed enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants