comparison php/read/getBookDetails.php @ 21:46382face560

use a semaphore to try to cut down on RequestThrottled fails
author Charlie Root
date Thu, 25 Oct 2018 09:42:12 -0400
parents c41a5802b26c
children d606320ec331
comparison
equal deleted inserted replaced
20:7679346abfdb 21:46382face560
202 $timeout = 5; 202 $timeout = 5;
203 curl_setopt ($crl, CURLOPT_URL,$url); 203 curl_setopt ($crl, CURLOPT_URL,$url);
204 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 204 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
205 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 205 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
206 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 206 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
207 $gotit=0;
208 $requestIP = $_SERVER['REMOTE_ADDR'];
209 $semaphore = new SyncSemaphore("Amazon");
210 $gotit = $semaphore->lock(4000);
211 if (!$gotit) {
212 file_put_contents('/var/ywww/debug/phpDebug',
213 "Got it: ".(int)$gotit." $requestIP 2\n",FILE_APPEND);
214 $gotit=$semaphore->lock(5000);
215 file_put_contents('/var/ywww/debug/phpDebug',
216 "Got it: ".(int)$gotit." $requestIP 2a\n",FILE_APPEND);
217 }
207 $out = curl_exec($crl); 218 $out = curl_exec($crl);
208 curl_close($crl); 219 curl_close($crl);
209 220 usleep(1000000);
221 if ($gotit) {
222 $semaphore->unlock();
223 }
224 else {
225 file_put_contents('/var/ywww/debug/phpDebug',
226 "W/o lock for $requestIP 2b\n",FILE_APPEND);
227 }
210 //echo $out; 228 //echo $out;
211 229
212 if (preg_match("<Error>",$out)) { 230 if (preg_match("<Error>",$out)) {
213 $xml = new SimpleXMLElement($out); 231 $xml = new SimpleXMLElement($out);
214 $resName=$xml->getName(); 232 $resName=$xml->getName();
215 $code=$xml->Error->Code; 233 $code=$xml->Error->Code;
216 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: $resName, $code\n",FILE_APPEND); 234 $requestIP = $_SERVER['REMOTE_ADDR'];
235 file_put_contents('/var/ywww/debug/phpDebug',"Losing gBD: $resName, $code, $requestIP\n",FILE_APPEND);
217 if ($code=='RequestThrottled') { 236 if ($code=='RequestThrottled') {
218 usleep(200000); // Try to reduce throttling until we get a 237 usleep(200000); // Try to reduce throttling until we get a
219 // principled solution in place 238 // principled solution in place
220 } 239 }
221 else { 240 else {