Skip to content

Add an optional retry delay#2

Open
beryllium wants to merge 2 commits intoigorw:masterfrom
beryllium:patch-1
Open

Add an optional retry delay#2
beryllium wants to merge 2 commits intoigorw:masterfrom
beryllium:patch-1

Conversation

@beryllium
Copy link
Copy Markdown

By specifying an optional 3rd parameter (in seconds), you can cause a small delay between retries. This can be useful to avoid the perception of flooding.

By specifying an optional 3rd parameter (in seconds), you can cause a small delay between retries. This can be useful to avoid the perception of flooding.
@igorw
Copy link
Copy Markdown
Owner

igorw commented Sep 22, 2014

I'm inclined to keep this lib as simple and small as possible. I am however considering adding a delay, or even a generic onError mechanism.

@Seldaek
Copy link
Copy Markdown

Seldaek commented Sep 22, 2014

+1 for usleep. Some things fix themselves rather quickly :) Third param could accept int microseconds for delay or a callable for custom onError?

@jmikola
Copy link
Copy Markdown

jmikola commented Sep 22, 2014

Third param could accept int microseconds for delay or a callable for custom onError?

👍 for keeping this flexible. While the general case will be for a fixed delay, there are cases where you might want to implement an exponential backoff (e.g. doctrine/mongodb#120 (comment)) or some other logic.

@beryllium
Copy link
Copy Markdown
Author

Yeah, I like the callable idea. Not sure about calling it onError, though - maybe onRetry?

@igorw
Copy link
Copy Markdown
Owner

igorw commented Sep 22, 2014

Here's my WIP proposal btw https://github.com/igorw/retry/compare/on-error

@igorw igorw mentioned this pull request Sep 22, 2014
@yitznewton
Copy link
Copy Markdown

@beryllium or maybe, beforeRetry

@jmikola
Copy link
Copy Markdown

jmikola commented Sep 23, 2014

@igorw: Whipped up the following, which I think would work nicely with the $onError callback:

<?php

class ExponentialBackoff
{
    private $invoked = 0;
    private $slotTimeUsec;

    public function __construct($slotTimeUsec)
    {
        $this->slotTimeUsec = (int) $slotTimeUsec;
    }

    public function __invoke()
    {
        usleep(rand(0, (2 << (++$this->invoked - 1)) - 1) * $this->slotTimeUsec);
    }
}

Missing bounds detection for the bit-shifting (probably to cap it at PHP_INT_MAX), but it implements the general formula from http://en.wikipedia.org/wiki/Exponential_backoff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants