X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=plugins%2FGeonamesPlugin.php;h=3815a31fa681d90ed8b1fb8782f4791918bd18f4;hb=6832a34216437cbacdc1e21f2cf8fa8f4e58ff63;hp=589462ed9936399492436536a8c5ca248e96ca4b;hpb=779204b194447397d0770d96e291d9491fd731b9;p=quix0rs-gnu-social.git diff --git a/plugins/GeonamesPlugin.php b/plugins/GeonamesPlugin.php index 589462ed99..3815a31fa6 100644 --- a/plugins/GeonamesPlugin.php +++ b/plugins/GeonamesPlugin.php @@ -55,6 +55,12 @@ 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 @@ -370,7 +376,7 @@ class GeonamesPlugin extends Plugin return true; } - $url = 'http://sw.geonames.org/' . $location->location_id . '/'; + $url = 'http://sws.geonames.org/' . $location->location_id . '/'; // it's been filled, so don't process further. return false; @@ -408,9 +414,14 @@ class GeonamesPlugin extends Plugin function cacheKey($attrs) { - return common_cache_key('geonames:'. - implode(',', array_keys($attrs)) . ':'. - common_keyize(implode(',', array_values($attrs)))); + $key = 'geonames:' . + implode(',', array_keys($attrs)) . ':'. + common_keyize(implode(',', array_values($attrs))); + if ($this->cachePrefix) { + return $this->cachePrefix . ':' . $key; + } else { + return common_cache_key($key); + } } function wsUrl($method, $params) @@ -430,12 +441,24 @@ 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); + throw new Exception("HTTP error code " . $result->getStatus()); } $body = $result->getBody();