@@ -32,13 +32,6 @@ class Bucket
3232 */
3333 protected $ queue ;
3434
35- /**
36- * Interaction request queue.
37- *
38- * @var SplQueue
39- */
40- protected $ interactionQueue ;
41-
4235 /**
4336 * Bucket name.
4437 *
@@ -109,7 +102,6 @@ class Bucket
109102 public function __construct (string $ name , LoopInterface $ loop , LoggerInterface $ logger , callable $ runRequest )
110103 {
111104 $ this ->queue = new SplQueue ;
112- $ this ->interactionQueue = new SplQueue ;
113105 $ this ->name = $ name ;
114106 $ this ->loop = $ loop ;
115107 $ this ->logger = $ logger ;
@@ -125,9 +117,7 @@ public function __construct(string $name, LoopInterface $loop, LoggerInterface $
125117 */
126118 public function enqueue (Request $ request )
127119 {
128- Http::isInteractionEndpoint ($ request )
129- ? $ this ->interactionQueue ->enqueue ($ request )
130- : $ this ->queue ->enqueue ($ request );
120+ $ this ->queue ->enqueue ($ request );
131121 $ this ->logger ->debug ($ this .' queued ' .$ request );
132122 $ this ->checkQueue ();
133123 }
@@ -142,92 +132,88 @@ public function checkQueue()
142132 return ;
143133 }
144134
145- $ this ->checkerRunning = true ;
146- $ this ->__checkQueue ();
147- }
148-
149- protected function __checkQueue (): void
150- {
151- // Check for rate-limits
152- if ($ this ->requestRemaining < 1 && ! is_null ($ this ->requestRemaining )) {
153- $ interval = ($ this ->resetTimer ) ? $ this ->resetTimer ->getInterval () ?? 0 : 0 ;
154- $ this ->logger ->info ($ this .' expecting rate limit, timer interval ' .($ interval * 1000 ).' ms ' );
155- $ this ->checkerRunning = false ;
135+ $ checkQueue = function () use (&$ checkQueue ) {
136+ // Check for rate-limits
137+ if ($ this ->requestRemaining < 1 && ! is_null ($ this ->requestRemaining )) {
138+ $ interval = 0 ;
139+ if ($ this ->resetTimer ) {
140+ $ interval = $ this ->resetTimer ->getInterval () ?? 0 ;
141+ }
142+ $ this ->logger ->info ($ this .' expecting rate limit, timer interval ' .($ interval * 1000 ).' ms ' );
143+ $ this ->checkerRunning = false ;
144+ $ checkQueue = null ;
156145
157- return ;
158- }
146+ return ;
147+ }
159148
160- // Queue is empty, job done.
161- if ($ this ->queue ->isEmpty () && ($ interactionQueueEmpty = $ this ->interactionQueue ->isEmpty ())) {
162- $ this ->checkerRunning = false ;
149+ // Queue is empty, job done.
150+ if ($ this ->queue ->isEmpty ()) {
151+ $ this ->checkerRunning = false ;
152+ $ checkQueue = null ;
163153
164- return ;
165- }
154+ return ;
155+ }
166156
167- /** @var Request */
168- $ request = ($ interactionQueueEmpty )
169- ? $ this ->queue ->dequeue ()
170- : $ this ->interactionQueue ->dequeue ();
157+ /** @var Request */
158+ $ request = $ this ->queue ->dequeue ();
171159
172- $ this ->__runRequest ($ request );
173- }
160+ // Promises v3 changed `->then` to behave as `->done` and removed `->then`. We still need the behaviour of `->done` in projects using v2
161+ ($ this ->runRequest )($ request )->{$ this ->promiseV3 ? 'then ' : 'done ' }(function (ResponseInterface $ response ) use (&$ checkQueue ) {
162+ $ resetAfter = (float ) $ response ->getHeaderLine ('X-Ratelimit-Reset-After ' );
163+ $ limit = $ response ->getHeaderLine ('X-Ratelimit-Limit ' );
164+ $ remaining = $ response ->getHeaderLine ('X-Ratelimit-Remaining ' );
174165
175- protected function __runRequest ($ request , bool $ interaction = false )
176- {
177- // Promises v3 changed `->then` to behave as `->done` and removed `->then`. We still need the behaviour of `->done` in projects using v2
178- ($ this ->runRequest )($ request )->{$ this ->promiseV3 ? 'then ' : 'done ' }(function (ResponseInterface $ response ) {
179- $ resetAfter = (float ) $ response ->getHeaderLine ('X-Ratelimit-Reset-After ' );
180- $ limit = $ response ->getHeaderLine ('X-Ratelimit-Limit ' );
181- $ remaining = $ response ->getHeaderLine ('X-Ratelimit-Remaining ' );
166+ if ($ resetAfter ) {
167+ $ resetAfter = (float ) $ resetAfter ;
182168
183- if ($ resetAfter ) {
184- $ resetAfter = (float ) $ resetAfter ;
169+ if ($ this ->resetTimer ) {
170+ $ this ->loop ->cancelTimer ($ this ->resetTimer );
171+ }
185172
186- if ($ this ->resetTimer ) {
187- $ this ->loop ->cancelTimer ($ this ->resetTimer );
173+ $ this ->resetTimer = $ this ->loop ->addTimer ($ resetAfter , function () {
174+ // Reset requests remaining and check queue
175+ $ this ->requestRemaining = $ this ->requestLimit ;
176+ $ this ->resetTimer = null ;
177+ $ this ->checkQueue ();
178+ });
188179 }
189180
190- $ this ->resetTimer = $ this ->loop ->addTimer ($ resetAfter , function () {
191- // Reset requests remaining and check queue
192- $ this ->requestRemaining = $ this ->requestLimit ;
193- $ this ->resetTimer = null ;
194- $ this ->checkQueue ();
195- });
196- }
197-
198- // Check if rate-limit headers are present and store
199- if (is_numeric ($ limit )) {
200- $ this ->requestLimit = (int ) $ limit ;
201- }
202-
203- if (is_numeric ($ remaining )) {
204- $ this ->requestRemaining = (int ) $ remaining ;
205- }
181+ // Check if rate-limit headers are present and store
182+ if (is_numeric ($ limit )) {
183+ $ this ->requestLimit = (int ) $ limit ;
184+ }
206185
207- // Check for more requests
208- $ this ->__checkQueue ();
209- }, function ($ rateLimit ) use ($ request ) {
210- if ($ rateLimit instanceof RateLimit) {
211- Http::isInteractionEndpoint ($ request )
212- ? $ this ->interactionQueue ->enqueue ($ request )
213- : $ this ->queue ->enqueue ($ request );
214-
215- // Bucket-specific rate-limit
216- // Re-queue the request and wait the retry after time
217- if (! $ rateLimit ->isGlobal ()) {
218- $ this ->loop ->addTimer ($ rateLimit ->getRetryAfter (), $ this ->__checkQueue ());
186+ if (is_numeric ($ remaining )) {
187+ $ this ->requestRemaining = (int ) $ remaining ;
219188 }
220- // Stop the queue checker for a global rate-limit.
221- // Will be restarted when global rate-limit finished.
222- else {
223- $ this ->checkerRunning = false ;
224189
225- $ this ->logger ->debug ($ this .' stopping queue checker ' );
190+ // Check for more requests
191+ $ checkQueue ();
192+ }, function ($ rateLimit ) use (&$ checkQueue , $ request ) {
193+ if ($ rateLimit instanceof RateLimit) {
194+ $ this ->queue ->enqueue ($ request );
195+
196+ // Bucket-specific rate-limit
197+ // Re-queue the request and wait the retry after time
198+ if (! $ rateLimit ->isGlobal ()) {
199+ $ this ->loop ->addTimer ($ rateLimit ->getRetryAfter (), $ checkQueue );
200+ }
201+ // Stop the queue checker for a global rate-limit.
202+ // Will be restarted when global rate-limit finished.
203+ else {
204+ $ this ->checkerRunning = false ;
205+ $ checkQueue = null ;
206+
207+ $ this ->logger ->debug ($ this .' stopping queue checker ' );
208+ }
209+ } else {
210+ $ checkQueue ();
226211 }
227- } else {
228- $ this ->__checkQueue ();
229- }
230- });
212+ });
213+ };
214+
215+ $ this ->checkerRunning = true ;
216+ $ checkQueue ();
231217 }
232218
233219 /**
0 commit comments