summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrion Vibber <brion@pobox.com>2010-04-06 15:14:28 -0700
committerBrion Vibber <brion@pobox.com>2010-04-06 15:14:28 -0700
commitcda03ff328814a3cc65eb418020924da74d9458a (patch)
tree8c635a89de870095203fa3789e4f1d84a0fa1134
parent7c3b320a7a28cdc6fcab9b5c4854bba0775d6ec2 (diff)
Set a default 2-second timeout on Geonames web service lookups. After a failure, further lookups in the same process will be skipped for the next 60 seconds (also configurable).
Makes a Geonames outage much less disruptive to site operations.
-rw-r--r--plugins/GeonamesPlugin.php18
1 files changed, 17 insertions, 1 deletions
diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php
index f018e2694..718af9e0b 100644
--- a/plugins/GeonamesPlugin.php
+++ b/plugins/GeonamesPlugin.php
@@ -55,9 +55,13 @@ class GeonamesPlugin extends Plugin
public $username = null;
public $token = null;
public $expiry = 7776000; // 90-day expiry
+ public $timeout = 2; // Web service timeout in seconds.
+ public $timeoutWindow = 60; // Further lookups in this process will be disabled for N seconds after a timeout.
public $cachePrefix = null; // Optional shared memcache prefix override
// to share lookups between local instances.
+ protected $lastTimeout = null; // timestamp of last web service timeout
+
/**
* convert a name into a Location object
*
@@ -437,9 +441,21 @@ class GeonamesPlugin extends Plugin
function getGeonames($method, $params)
{
+ if ($this->lastTimeout && (time() - $this->lastTimeout < $this->timeoutWindow)) {
+ throw new Exception("skipping due to recent web service timeout");
+ }
+
$client = HTTPClient::start();
+ $client->setConfig('connect_timeout', $this->timeout);
+ $client->setConfig('timeout', $this->timeout);
- $result = $client->get($this->wsUrl($method, $params));
+ try {
+ $result = $client->get($this->wsUrl($method, $params));
+ } catch (Exception $e) {
+ common_log(LOG_ERR, __METHOD__ . ": " . $e->getMessage());
+ $this->lastTimeout = time();
+ throw $e;
+ }
if (!$result->isOk()) {
throw new Exception("HTTP error code " . $result->code);