@@ -132,88 +132,86 @@ public function checkQueue()
132132 return ;
133133 }
134134
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 ;
135+ $ this ->checkerRunning = true ;
136+ $ this ->__checkQueue ();
137+ }
145138
146- return ;
139+ protected function __checkQueue ()
140+ {
141+ // Check for rate-limits
142+ if ($ this ->requestRemaining < 1 && ! is_null ($ this ->requestRemaining )) {
143+ $ interval = 0 ;
144+ if ($ this ->resetTimer ) {
145+ $ interval = $ this ->resetTimer ->getInterval () ?? 0 ;
147146 }
147+ $ this ->logger ->info ($ this .' expecting rate limit, timer interval ' .($ interval * 1000 ).' ms ' );
148+ $ this ->checkerRunning = false ;
148149
149- // Queue is empty, job done.
150- if ($ this ->queue ->isEmpty ()) {
151- $ this ->checkerRunning = false ;
152- $ checkQueue = null ;
150+ return ;
151+ }
153152
154- return ;
155- }
153+ // Queue is empty, job done.
154+ if ($ this ->queue ->isEmpty ()) {
155+ $ this ->checkerRunning = false ;
156156
157- /** @var Request */
158- $ request = $ this -> queue -> dequeue ();
157+ return ;
158+ }
159159
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 ' );
160+ /** @var Request */
161+ $ request = $ this ->queue ->dequeue ();
165162
166- if ($ resetAfter ) {
167- $ resetAfter = (float ) $ resetAfter ;
163+ // Promises v3 changed `->then` to behave as `->done` and removed `->then`. We still need the behaviour of `->done` in projects using v2
164+ ($ this ->runRequest )($ request )->{$ this ->promiseV3 ? 'then ' : 'done ' }(function (ResponseInterface $ response ) {
165+ $ resetAfter = (float ) $ response ->getHeaderLine ('X-Ratelimit-Reset-After ' );
166+ $ limit = $ response ->getHeaderLine ('X-Ratelimit-Limit ' );
167+ $ remaining = $ response ->getHeaderLine ('X-Ratelimit-Remaining ' );
168168
169- if ($ this ->resetTimer ) {
170- $ this ->loop ->cancelTimer ($ this ->resetTimer );
171- }
169+ if ($ resetAfter ) {
170+ $ resetAfter = (float ) $ resetAfter ;
172171
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- });
172+ if ($ this ->resetTimer ) {
173+ $ this ->loop ->cancelTimer ($ this ->resetTimer );
179174 }
180175
181- // Check if rate-limit headers are present and store
182- if (is_numeric ($ limit )) {
183- $ this ->requestLimit = (int ) $ limit ;
184- }
176+ $ this ->resetTimer = $ this ->loop ->addTimer ($ resetAfter , function () {
177+ // Reset requests remaining and check queue
178+ $ this ->requestRemaining = $ this ->requestLimit ;
179+ $ this ->resetTimer = null ;
180+ $ this ->checkQueue ();
181+ });
182+ }
185183
186- if (is_numeric ($ remaining )) {
187- $ this ->requestRemaining = (int ) $ remaining ;
188- }
184+ // Check if rate-limit headers are present and store
185+ if (is_numeric ($ limit )) {
186+ $ this ->requestLimit = (int ) $ limit ;
187+ }
188+
189+ if (is_numeric ($ remaining )) {
190+ $ this ->requestRemaining = (int ) $ remaining ;
191+ }
192+
193+ // Check for more requests
194+ $ this ->__checkQueue ();
195+ }, function ($ rateLimit ) use ($ request ) {
196+ if ($ rateLimit instanceof RateLimit) {
197+ $ this ->queue ->enqueue ($ request );
189198
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 ();
199+ // Bucket-specific rate-limit
200+ // Re-queue the request and wait the retry after time
201+ if (! $ rateLimit ->isGlobal ()) {
202+ $ this ->loop ->addTimer ($ rateLimit ->getRetryAfter (), fn () => $ this ->__checkQueue ());
211203 }
212- });
213- };
204+ // Stop the queue checker for a global rate-limit.
205+ // Will be restarted when global rate-limit finished.
206+ else {
207+ $ this ->checkerRunning = false ;
214208
215- $ this ->checkerRunning = true ;
216- $ checkQueue ();
209+ $ this ->logger ->debug ($ this .' stopping queue checker ' );
210+ }
211+ } else {
212+ $ this ->__checkQueue ();
213+ }
214+ });
217215 }
218216
219217 /**
0 commit comments