comparison xml/doAmazonRequest.php @ 53:dd93cb4b77ad

more debugging (too much for production), fallback to DB if getBookDetails is knocked back by Amazon
author Charlie Root
date Thu, 06 Jun 2019 16:37:20 -0400
parents a67bf725e87b
children 72708ec9c0e6
comparison
equal deleted inserted replaced
52:d9dd9705e724 53:dd93cb4b77ad
16 } 16 }
17 $gotit=0; 17 $gotit=0;
18 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); 18 $url=aws_signed_request($ext,$parameters,$public_key,$private_key);
19 //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n"); 19 //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n");
20 for ($i=1; $i<=$try; $i++) { 20 for ($i=1; $i<=$try; $i++) {
21 dl("dar: $requestIP $i\n");
21 $crl = curl_init(); 22 $crl = curl_init();
22 $timeout = 5; 23 $timeout = 5;
23 curl_setopt ($crl, CURLOPT_URL,$url); 24 curl_setopt ($crl, CURLOPT_URL,$url);
24 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 25 curl_setopt ($crl, CURLOPT_ENCODING , "gzip");
25 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); 26 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
26 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); 27 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
27 $semaphore = new SyncSemaphore("Amazon"); 28 $semaphore = new SyncSemaphore("Amazon".$ext);
28 $gotit = $semaphore->lock(1000); 29 $gotit = $semaphore->lock(5000);
29 if (!$gotit) { 30 if (!$gotit) {
30 dl("Got it: ".(int)$gotit." $requestIP $dbl\n"); 31 dl("No lock: $requestIP $dbl\n");
31 $gotit=$semaphore->lock(1000); 32 for ($j=1; $j<=$try+4; $j++) {
32 dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n"); 33 dl("dar: $requestIP $i $j\n");
33 } 34 $gotit=$semaphore->lock(1000);
34 $output = curl_exec($crl); 35 if ($gotit) {
35 //dl("nai2: ".strlen($output)."\n"); 36 dl("Got it: $i $j $requestIP $dbl"."a\n");
36 37 break;
37 curl_close($crl); 38 }
38 usleep(500000); 39 }
39 if ($gotit) {
40 $semaphore->unlock();
41 } 40 }
42 else { 41 else {
43 dl("W/o lock for $requestIP $dbl"."b\n"); 42 dl("Got it: $i ! $requestIP $dbl"."a\n");
44 } 43 }
44 if (!$gotit) {
45 usleep(1000000);
46 continue;
47 }
48 usleep(1000000);
49 $output = curl_exec($crl);
50 dl("nai2: $gotit ".strlen($output)."\n");
51 curl_close($crl);
45 $mm=array(); 52 $mm=array();
46 $xml = new SimpleXMLElement($output); 53 $xml = new SimpleXMLElement($output);
47 if (preg_match("/<Error>/",$output,$mm)) { 54 if (preg_match("/<Error>/",$output,$mm)) {
48 $resName=$xml->getName(); 55 $resName=$xml->getName();
49 $code=$xml->Error->Code; 56 $code=$xml->Error->Code;
66 $parameters['ItemId']=$isbnMaybe; 73 $parameters['ItemId']=$isbnMaybe;
67 if (isset($parameters['Keywords'])) { 74 if (isset($parameters['Keywords'])) {
68 $parameters['Keywords']=urlencode($isbnMaybe); 75 $parameters['Keywords']=urlencode($isbnMaybe);
69 } 76 }
70 dl("retrying with $isbnMaybe\n"); 77 dl("retrying with $isbnMaybe\n");
78 if ($gotit) {
79 $semaphore->unlock();
80 }
71 return doAmazonRequest($ext,$parameters,$try,$dbl); 81 return doAmazonRequest($ext,$parameters,$try,$dbl);
72 } 82 }
73 } 83 }
74 if ($code=="") { 84 if ($code=="") {
75 dl("error elt:\n$output\n"); 85 dl("error elt:\n$output\n");
76 } 86 }
87 if ($gotit) {
88 $semaphore->unlock();
89 }
77 throw new Exception($code); 90 throw new Exception($code);
78 } 91 }
79 } 92 }
80 else { 93 else {
94 if ($gotit) {
95 $cnt=0;
96 $semaphore->unlock($cnt);
97 dl("unlocked: $i $cnt\n");
98 }
81 if ($wantXML) { 99 if ($wantXML) {
82 return $xml; 100 return $xml;
83 } 101 }
84 else { 102 else {
85 //dl("returning ".strlen($output)."\n"); 103 //dl("returning ".strlen($output)."\n");
86 return $output; 104 return $output;
87 } 105 }
88 } 106 }
89 usleep(100000*$i); // Try to reduce throttling until we get a 107 usleep(100000*$i); // Try to reduce throttling until we get a
90 // principled solution in place 108 // principled solution in place
109 if ($gotit) {
110 $semaphore->unlock();
111 }
91 } 112 }
92 throw new Exception("ThrottledRepeatedly"); 113 throw new Exception("ThrottledRepeatedly");
93 } 114 }
94 ?> 115 ?>