| 1 | <?php |
||
| 5 | class Throttler { |
||
| 6 | |||
| 7 | /** |
||
| 8 | * @var float|null |
||
| 9 | */ |
||
| 10 | private $lastTime = null; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * interval to wait in seconds, can be float |
||
| 14 | * |
||
| 15 | * @var float |
||
| 16 | */ |
||
| 17 | private $interval; |
||
| 18 | |||
| 19 | 4 | public function __construct($interval) { |
|
| 22 | |||
| 23 | 6 | public function waitForThrottle() { |
|
| 24 | 6 | if (!$this->lastTime) { |
|
| 25 | 3 | $this->lastTime = \microtime(true); |
|
| 26 | 3 | return; |
|
| 27 | } |
||
| 28 | |||
| 29 | 5 | $diff = $this->interval - (\microtime(true) - $this->lastTime); |
|
| 30 | |||
| 31 | 5 | if ($diff > 0) { |
|
| 32 | 1 | usleep((int)ceil($diff * 1000 * 1000)); |
|
| 33 | } |
||
| 34 | |||
| 35 | 5 | $this->lastTime = \microtime(true); |
|
| 36 | 5 | } |
|
| 37 | |||
| 38 | private static $instances = []; |
||
| 39 | |||
| 40 | 80 | public static function getInstance($key, $interval) { |
|
| 47 | } |
||
| 48 |