LoginSignup
1
2

More than 3 years have passed since last update.

良さげリトライ機能の実装の仕方

Posted at

Laravelのソースコードを呼んでいたら良さげなリトライ実装がされていたのでメモ

    function retry($times, callable $callback, $sleep = 0)
    {
        $attempts = 0;
        $times--;

        beginning:
        $attempts++;

        try {
            return $callback($attempts);
        } catch (Exception $e) {
            if (! $times) {
                throw $e;
            }

            $times--;

            if ($sleep) {
                usleep($sleep * 1000);
            }

            goto beginning;
        }
    }
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2