diff 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
line wrap: on
line diff
--- a/xml/getAmazonInfo.php	Thu Oct 25 09:40:25 2018 -0400
+++ b/xml/getAmazonInfo.php	Thu Oct 25 09:42:12 2018 -0400
@@ -83,30 +83,54 @@
 	'ReviewSort'	  =>'-HelpfulVotes'
   );
 
+$requestURI = $_SERVER['REQUEST_URI'];
+$requestIP = $_SERVER['REMOTE_ADDR'];
+// if ($requestIP=="173.161.113.65" || $requestIP=="141.8.132.25") {
+// 	    $delay=60;
+// 	    file_put_contents('/var/ywww/debug/phpDebug',
+// 			      "bad guy: $requestIP, $requestURI\n",
+// 			      FILE_APPEND);
+// 	    sleep($delay);
+// 	    # No, can't do this
+// 	    # return; # bomb!
+// 	    # 'Kung', sitting on my desk in the office while I'm at home,
+// 	    # is occasionally hitting xml/getAmazonInfo.php:
+// 	    # e.g. Losing: ItemLookupErrorResponse, RequestThrottled, 129.215.197.36, /xml/getAmazonInfo.php?searchparameterdata=075154454X&locale=uk
+// 	    # repeatedly, same params
+// }
 $ext=$Aserver[$locale]['ext'];  
 $file_data=$ext;
 ksort($parameters);
 foreach ($parameters as $i=>$d) {
   $file_data.='&'.$i.'='.$d;
 }
-
-$url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
-//echo $url;
+        $gotit=0;
+        $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
         $crl = curl_init();
         $timeout = 5;
         curl_setopt ($crl, CURLOPT_URL,$url);
 		curl_setopt ($crl, CURLOPT_ENCODING , "gzip"); 
         curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, $timeout);
-	/*	$timeDiff=((int)(microtime(true)*1000))-$lastReqTime;
-	if ($timeDiff<1000) {
-	  usleep((1000-$timeDiff)*1000); // microseconds
-	  }*/
+	$semaphore = new SyncSemaphore("Amazon");
+	$gotit = $semaphore->lock(4000);
+	if (!$gotit) {
+	  file_put_contents('/var/ywww/debug/phpDebug',
+			"Got it: ".(int)$gotit." $requestIP 1\n",FILE_APPEND);
+	  $gotit=$semaphore->lock(5000);
+	  file_put_contents('/var/ywww/debug/phpDebug',
+		        "Got it: ".(int)$gotit." $requestIP 1a\n",FILE_APPEND);
+	}
         $output = curl_exec($crl);
-	//$lastReqTime=(int)(microtime(true)*1000); // milliseconds
         curl_close($crl);
-		
-	/*$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>");*/
+	usleep(1000000);
+	if ($gotit) {
+	  $semaphore->unlock();
+	}
+	else {
+	  file_put_contents('/var/ywww/debug/phpDebug',
+			    "W/o lock for $requestIP 1b\n",FILE_APPEND);
+	}
 	$review = "";
 	$review1 = "";
 	$review2 = "";
@@ -116,10 +140,10 @@
 	  $xml = new SimpleXMLElement($output);	
 	  $resName=$xml->getName();
 	  $code=$xml->Error->Code;
-	  file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, $code\n",FILE_APPEND);
+	  file_put_contents('/var/ywww/debug/phpDebug',"Losing: $resName, $code, $requestIP, $requestURI\n",FILE_APPEND);
 	  if ($code=='RequestThrottled') {
-	    usleep(200000); // Try to reduce throttling until we get a 
-	    // principled solution in place
+	      sleep(5); // Try to reduce throttling until we get a 
+	      // principled solution in place
 	  }
 	  else {
 	     file_put_contents('/var/ywww/debug/phpDebug',"parms: $go\nurl: $url\n".
@@ -590,4 +614,5 @@
 
 )
 */
+	/*$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>");*/
 ?>