-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimized
retry
helper function can accept array as first parameter…
…. (#6539) Co-authored-by: 李铭昕 <[email protected]>
- Loading branch information
1 parent
5f3bb35
commit f59b668
Showing
4 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
namespace Hyperf\Support\Backoff; | ||
|
||
use Hyperf\Collection\Arr; | ||
use InvalidArgumentException; | ||
|
||
class ArrayBackoff | ||
{ | ||
private int $lastMillisecond; | ||
|
||
/** | ||
* @param array $milliseconds backoff interval | ||
*/ | ||
public function __construct(private array $milliseconds) | ||
{ | ||
if (empty($milliseconds)) { | ||
throw new InvalidArgumentException( | ||
'The backoff interval milliseconds cannot be empty.' | ||
); | ||
} | ||
|
||
$this->lastMillisecond = (int) array_pop($this->milliseconds); | ||
} | ||
|
||
/** | ||
* Sleep until the next execution. | ||
*/ | ||
public function sleep(): void | ||
{ | ||
$ms = (int) (array_shift($this->milliseconds) ?? $this->lastMillisecond); | ||
|
||
if ($ms === 0) { | ||
return; | ||
} | ||
|
||
usleep($ms * 1000); | ||
} | ||
|
||
/** | ||
* Get the next backoff for logging, etc. | ||
* @return int next backoff | ||
*/ | ||
public function nextBackoff(): int | ||
{ | ||
return (int) Arr::first($this->milliseconds, default: $this->lastMillisecond); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters