Skip to content

Commit b25dcb6

Browse files
author
Tyler King
authored
Merge pull request #120 from roberttolton/master
Fixes REST API Rate Limiting
2 parents 174a45e + 8e8f8a4 commit b25dcb6

File tree

3 files changed

+35
-8
lines changed

3 files changed

+35
-8
lines changed

src/Middleware/RateLimiting.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ protected function handleRest(BasicShopifyAPI $api): bool
5050
$ts = $client->getTimeStore();
5151

5252
$times = $ts->get($api->getSession());
53-
if (count($times) !== $api->getOptions()->getRestLimit()) {
53+
if (count($times) < $api->getOptions()->getRestLimit()) {
5454
// Not at our limit yet, allow through without limiting
5555
return false;
5656
}
5757

5858
// Determine if this call has passed the window
59-
$firstTime = end($times);
59+
$firstTime = end($times)['time'];
6060
$windowTime = $firstTime + 1000000;
6161
$currentTime = $td->getCurrentTime();
6262

src/Middleware/UpdateApiLimits.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ protected function updateRestLimits(ResponseInterface $response): void
107107
'left' => (int) $calls[1] - (int) $calls[0],
108108
'made' => (int) $calls[0],
109109
'limit' => (int) $calls[1],
110+
'time' => $client->getTimeDeferrer()->getCurrentTime(),
110111
],
111112
$this->api->getSession()
112113
);

test/Middleware/RateLimitingTest.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,25 @@ public function testRestSleep(): void
2222
// Create fake times
2323
$td = $api->getRestClient()->getTimeDeferrer();
2424
$currentTime = $td->getCurrentTime();
25-
$firstTime = $currentTime - 200;
26-
$lastTime = $currentTime - 100;
25+
$firstTime = $currentTime - 500;
26+
$secondTime = $currentTime - 300;
2727

2828
// Fill fake times
2929
$ts = $api->getRestClient()->getTimeStore();
30-
$ts->set([$lastTime, $firstTime], $api->getSession());
30+
$ts->set([
31+
[
32+
'left' => 38,
33+
'made' => 2,
34+
'limit' => 40,
35+
'time' => $secondTime,
36+
],
37+
[
38+
'left' => 39,
39+
'made' => 1,
40+
'limit' => 40,
41+
'time' => $firstTime,
42+
]
43+
], $api->getSession());
3144

3245
// Given we have 2 previous calls within 1 second window, sleep should trigger
3346
$result = $method->invoke(new RateLimiting($api), $api);
@@ -47,12 +60,25 @@ public function testRestNoSleep(): void
4760
// Create fake times
4861
$td = $api->getRestClient()->getTimeDeferrer();
4962
$currentTime = $td->getCurrentTime();
50-
$firstTime = $currentTime - 200;
51-
$lastTime = $currentTime - 100;
63+
$firstTime = $currentTime - 500;
64+
$secondTime = $currentTime - 300;
5265

5366
// Fill fake times
5467
$ts = $api->getRestClient()->getTimeStore();
55-
$ts->set([$lastTime, $firstTime], $api->getSession());
68+
$ts->set([
69+
[
70+
'left' => 38,
71+
'made' => 2,
72+
'limit' => 40,
73+
'time' => $secondTime,
74+
],
75+
[
76+
'left' => 39,
77+
'made' => 1,
78+
'limit' => 40,
79+
'time' => $firstTime,
80+
]
81+
], $api->getSession());
5682

5783
// Even though two requests happened within 1 second window,
5884
// If we do the "next" call after the window time, it should

0 commit comments

Comments
 (0)