retry icon indicating copy to clipboard operation
retry copied to clipboard

Add an optional retry delay

Open beryllium opened this issue 10 years ago • 7 comments

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.

beryllium avatar Sep 19 '14 22:09 beryllium

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.

igorw avatar Sep 22 '14 07:09 igorw

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

Seldaek avatar Sep 22 '14 12:09 Seldaek

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

:+1: 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. https://github.com/doctrine/mongodb/issues/120#issuecomment-21824747) or some other logic.

jmikola avatar Sep 22 '14 17:09 jmikola

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

beryllium avatar Sep 22 '14 20:09 beryllium

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

igorw avatar Sep 22 '14 21:09 igorw

@beryllium or maybe, beforeRetry

yitznewton avatar Sep 22 '14 22:09 yitznewton

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

jmikola avatar Sep 23 '14 04:09 jmikola