diff 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
line wrap: on
line diff
--- a/xml/doAmazonRequest.php	Thu Jun 06 16:33:34 2019 -0400
+++ b/xml/doAmazonRequest.php	Thu Jun 06 16:37:20 2019 -0400
@@ -18,30 +18,37 @@
   $url=aws_signed_request($ext,$parameters,$public_key,$private_key);  
   //dl("search: $ext $public_key $private_key\n$url\n".print_r($parameters,TRUE)."\n");
   for ($i=1; $i<=$try; $i++) {
+    dl("dar: $requestIP $i\n");
     $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);
-    $semaphore = new SyncSemaphore("Amazon");
-    $gotit = $semaphore->lock(1000);
+    $semaphore = new SyncSemaphore("Amazon".$ext);
+    $gotit = $semaphore->lock(5000);
     if (!$gotit) {
-      dl("Got it: ".(int)$gotit." $requestIP $dbl\n");
-      $gotit=$semaphore->lock(1000);
-      dl("Got it: ".(int)$gotit." $requestIP $dbl"."a\n");
-    }
-    $output = curl_exec($crl);
-    //dl("nai2: ".strlen($output)."\n");
-
-    curl_close($crl);
-    usleep(500000);
-    if ($gotit) {
-      $semaphore->unlock();
+      dl("No lock: $requestIP $dbl\n");
+      for ($j=1; $j<=$try+4; $j++) {
+	dl("dar: $requestIP $i $j\n");
+	$gotit=$semaphore->lock(1000);
+	if ($gotit) {
+	  dl("Got it: $i $j $requestIP $dbl"."a\n");
+	  break;
+	}
+      }
     }
     else {
-      dl("W/o lock for $requestIP $dbl"."b\n");
+      dl("Got it: $i ! $requestIP $dbl"."a\n");
     }
+    if (!$gotit) {
+      usleep(1000000);
+      continue;
+    }
+    usleep(1000000);
+    $output = curl_exec($crl);
+    dl("nai2: $gotit ".strlen($output)."\n");
+    curl_close($crl);
     $mm=array();
     $xml = new SimpleXMLElement($output);	
     if (preg_match("/<Error>/",$output,$mm)) {
@@ -68,16 +75,27 @@
 		$parameters['Keywords']=urlencode($isbnMaybe);
 	      }
 	    dl("retrying with $isbnMaybe\n");
+	    if ($gotit) {
+	      $semaphore->unlock();
+	    }
 	    return doAmazonRequest($ext,$parameters,$try,$dbl);
 	  }
 	}
 	if ($code=="") {
 	  dl("error elt:\n$output\n");
 	}
+	if ($gotit) {
+	  $semaphore->unlock();
+	}
 	throw new Exception($code);
       }
     }
     else {
+      if ($gotit) {
+	$cnt=0;
+	$semaphore->unlock($cnt);
+	dl("unlocked: $i $cnt\n");
+      }
       if ($wantXML) {
 	return $xml;
       }
@@ -88,6 +106,9 @@
     }
     usleep(100000*$i); // Try to reduce throttling until we get a 
     // principled solution in place
+    if ($gotit) {
+      $semaphore->unlock();
+    }
   }
   throw new Exception("ThrottledRepeatedly");
 }