Skip to content

Commit 4c16f1d

Browse files
committed
feat(shopify): throttle requests using redis
1 parent eeb390f commit 4c16f1d

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

lib/CurlRequest.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static function get($url, $httpHeaders = array())
7171
//Initialize the Curl resource
7272
$ch = self::init($url, $httpHeaders);
7373

74-
return self::processRequest($ch);
74+
return self::processRequest($ch, $url);
7575
}
7676

7777
/**
@@ -90,7 +90,7 @@ public static function post($url, $data, $httpHeaders = array())
9090
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
9191
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
9292

93-
return self::processRequest($ch);
93+
return self::processRequest($ch, $url);
9494
}
9595

9696
/**
@@ -109,7 +109,7 @@ public static function put($url, $data, $httpHeaders = array())
109109
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
110110
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
111111

112-
return self::processRequest($ch);
112+
return self::processRequest($ch, $url);
113113
}
114114

115115
/**
@@ -126,7 +126,7 @@ public static function delete($url, $httpHeaders = array())
126126
//set the request type
127127
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
128128

129-
return self::processRequest($ch);
129+
return self::processRequest($ch, $url);
130130
}
131131

132132
/**
@@ -138,18 +138,32 @@ public static function delete($url, $httpHeaders = array())
138138
*
139139
* @return string
140140
*/
141-
protected static function processRequest($ch)
141+
protected static function processRequest($ch, $url)
142142
{
143-
$output = curl_exec($ch);
144-
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
143+
$split = parse_url($url);
144+
$host = $split['host'];
145+
146+
$output = null;
147+
while(1) {
148+
Redis::throttle($host)->allow(2)->every(1)->then(function () use (&$ch, &$output) {
149+
$output = curl_exec($ch);
150+
self::$lastHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
151+
},
152+
153+
function () {
154+
});
155+
156+
if (isset($output)) {
157+
break;
158+
}
159+
}
145160

146161
if (curl_errno($ch)) {
147162
throw new Exception\CurlException(curl_errno($ch) . ' : ' . curl_error($ch));
148163
}
149164

150165
// close curl resource to free up system resources
151166
curl_close($ch);
152-
153167
return $output;
154168
}
155169

0 commit comments

Comments
 (0)