Mercurial > hg > ywww
comparison xml/getAmazonInfo.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 | 84a412b8122c |
| children | d606320ec331 |
comparison
equal
deleted
inserted
replaced
| 20:7679346abfdb | 21:46382face560 |
|---|---|
| 81 'AssociateTag' =>$Aassociates_id[$locale], | 81 'AssociateTag' =>$Aassociates_id[$locale], |
| 82 'ResponseGroup' =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes' , //Small, Medium, Large or SellerListing,'BrowseNodes',// , | 82 'ResponseGroup' =>'ItemAttributes,Reviews,EditorialReview,OfferSummary,Offers,Images,AlternateVersions,SalesRank,BrowseNodes' , //Small, Medium, Large or SellerListing,'BrowseNodes',// , |
| 83 'ReviewSort' =>'-HelpfulVotes' | 83 'ReviewSort' =>'-HelpfulVotes' |
| 84 ); | 84 ); |
| 85 | 85 |
| 86 $requestURI = $_SERVER['REQUEST_URI']; | |
| 87 $requestIP = $_SERVER['REMOTE_ADDR']; | |
| 88 // if ($requestIP=="173.161.113.65" || $requestIP=="141.8.132.25") { | |
| 89 // $delay=60; | |
| 90 // file_put_contents('/var/ywww/debug/phpDebug', | |
| 91 // "bad guy: $requestIP, $requestURI\n", | |
| 92 // FILE_APPEND); | |
| 93 // sleep($delay); | |
| 94 // # No, can't do this | |
| 95 // # return; # bomb! | |
| 96 // # 'Kung', sitting on my desk in the office while I'm at home, | |
| 97 // # is occasionally hitting xml/getAmazonInfo.php: | |
| 98 // # e.g. Losing: ItemLookupErrorResponse, RequestThrottled, 129.215.197.36, /xml/getAmazonInfo.php?searchparameterdata=075154454X&locale=uk | |
| 99 // # repeatedly, same params | |
| 100 // } | |
| 86 $ext=$Aserver[$locale]['ext']; | 101 $ext=$Aserver[$locale]['ext']; |
| 87 $file_data=$ext; | 102 $file_data=$ext; |
| 88 ksort($parameters); | 103 ksort($parameters); |
| 89 foreach ($parameters as $i=>$d) { | 104 foreach ($parameters as $i=>$d) { |
| 90 $file_data.='&'.$i.'='.$d; | 105 $file_data.='&'.$i.'='.$d; |
| 91 } | 106 } |
| 92 | 107 $gotit=0; |
| 93 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); | 108 $url=aws_signed_request($ext,$parameters,$public_key,$private_key); |
| 94 //echo $url; | |
| 95 $crl = curl_init(); | 109 $crl = curl_init(); |
| 96 $timeout = 5; | 110 $timeout = 5; |
| 97 curl_setopt ($crl, CURLOPT_URL,$url); | 111 curl_setopt ($crl, CURLOPT_URL,$url); |
| 98 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); | 112 curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); |
| 99 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); | 113 curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1); |
| 100 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); | 114 curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout); |
| 101 /* $timeDiff=((int)(microtime(true)*1000))-$lastReqTime; | 115 $semaphore = new SyncSemaphore("Amazon"); |
| 102 if ($timeDiff<1000) { | 116 $gotit = $semaphore->lock(4000); |
| 103 usleep((1000-$timeDiff)*1000); // microseconds | 117 if (!$gotit) { |
| 104 }*/ | 118 file_put_contents('/var/ywww/debug/phpDebug', |
| 119 "Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND); | |
| 120 $gotit=$semaphore->lock(5000); | |
| 121 file_put_contents('/var/ywww/debug/phpDebug', | |
| 122 "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND); | |
| 123 } | |
| 105 $output = curl_exec($crl); | 124 $output = curl_exec($crl); |
| 106 //$lastReqTime=(int)(microtime(true)*1000); // milliseconds | |
| 107 curl_close($crl); | 125 curl_close($crl); |
| 108 | 126 usleep(1000000); |
| 109 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ | 127 if ($gotit) { |
| 128 $semaphore->unlock(); | |
| 129 } | |
| 130 else { | |
| 131 file_put_contents('/var/ywww/debug/phpDebug', | |
| 132 "W/o lock for $requestIP 1b\n",FILE_APPEND); | |
| 133 } | |
| 110 $review = ""; | 134 $review = ""; |
| 111 $review1 = ""; | 135 $review1 = ""; |
| 112 $review2 = ""; | 136 $review2 = ""; |
| 113 $review3 = ""; | 137 $review3 = ""; |
| 114 // HST added this | 138 // HST added this |
| 115 if (preg_match("<Error>",$output)) { | 139 if (preg_match("<Error>",$output)) { |
| 116 $xml = new SimpleXMLElement($output); | 140 $xml = new SimpleXMLElement($output); |
| 117 $resName=$xml->getName(); | 141 $resName=$xml->getName(); |
| 118 $code=$xml->Error->Code; | 142 $code=$xml->Error->Code; |
| 119 file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, $code\n",FILE_APPEND); | 143 file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, $code, $requestIP, $requestURI\n",FILE_APPEND); |
| 120 if ($code=='RequestThrottled') { | 144 if ($code=='RequestThrottled') { |
| 121 usleep(200000); // Try to reduce throttling until we get a | 145 sleep(5); // Try to reduce throttling until we get a |
| 122 // principled solution in place | 146 // principled solution in place |
| 123 } | 147 } |
| 124 else { | 148 else { |
| 125 file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n". | 149 file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n". |
| 126 print_r($parameters,TRUE)."\n",FILE_APPEND); | 150 print_r($parameters,TRUE)."\n",FILE_APPEND); |
| 127 if ($code=="") { | 151 if ($code=="") { |
| 588 | 612 |
| 589 ) | 613 ) |
| 590 | 614 |
| 591 ) | 615 ) |
| 592 */ | 616 */ |
| 617 /*$xml = new SimpleXMLElement("<?xml version=\"1.0\"?><ItemLookupErrorResponse xmlns=\"http://ecs.amazonaws.com/doc/2009-03-31/\"><Error><Code>AccountLimitExceeded</Code><Message>Account limit of 2056 requests per hour exceeded.</Message></Error><RequestID>290ed059-730c-4789-93b4-6d21e11053d3</RequestID></ItemLookupErrorResponse>");*/ | |
| 593 ?> | 618 ?> |
