From 9a728ac86558fbbdaaf89bccb9427d923f04e29a Mon Sep 17 00:00:00 2001 From: rcsoftware-it <45300685+rcsoftware-it@users.noreply.github.com> Date: Sat, 24 Nov 2018 00:48:25 +0100 Subject: [PATCH] dynamic throttle Discogs send remaining request per minute, maybe something else have made requests, using dynamic throttle instead of fixed one. private $throttle is not needed anymore. --- lib/Discogs/Subscriber/ThrottleSubscriber.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/Discogs/Subscriber/ThrottleSubscriber.php b/lib/Discogs/Subscriber/ThrottleSubscriber.php index fdabc3e..77e9c41 100644 --- a/lib/Discogs/Subscriber/ThrottleSubscriber.php +++ b/lib/Discogs/Subscriber/ThrottleSubscriber.php @@ -14,7 +14,6 @@ class ThrottleSubscriber implements SubscriberInterface { private $throttle; - private static $previousTimestamp; public function __construct($throttle = 1000000) { @@ -30,13 +29,13 @@ public function getEvents() public function onComplete(CompleteEvent $event) { - $now = microtime(true); - $wait = self::$previousTimestamp + $this->throttle - $now; + // dynamic throttle + $remaining = $event->getTransaction()->response->getHeader('X-Discogs-Ratelimit-Remaining'); + if (!$remaining) $remaining = 1; + $wait = (int)(60 / $remaining * 1000000); if ($wait > 0) { usleep($wait); } - - self::$previousTimestamp = microtime(true); } }