Merge pull request #4116 from nextcloud/swift-cache-token

Cache swift authentication token in memcache
This commit is contained in:
Roeland Jago Douma 2017-03-29 11:23:13 +02:00 committed by GitHub
commit 626d03e3d4
2 changed files with 63 additions and 11 deletions

@ -1 +1 @@
Subproject commit 540cad1fa70205ca38dd344ea09d412bd986a1e2
Subproject commit 18adb82bab030a0af41e66e421a90d123a21a8f2

View File

@ -31,6 +31,7 @@ use OCP\Files\StorageNotAvailableException;
use OpenCloud\Common\Exceptions\EndpointError;
use OpenCloud\Common\Service\Catalog;
use OpenCloud\Common\Service\CatalogItem;
use OpenCloud\Identity\Resource\Token;
use OpenCloud\ObjectStore\Service;
use OpenCloud\OpenStack;
use OpenCloud\Rackspace;
@ -57,6 +58,8 @@ class Swift implements IObjectStore {
*/
private $container;
private $memcache;
public function __construct($params) {
if (isset($params['bucket'])) {
$params['container'] = $params['bucket'];
@ -71,9 +74,15 @@ class Swift implements IObjectStore {
if (isset($params['apiKey'])) {
$this->client = new Rackspace($params['url'], $params);
$cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket'];
} else {
$this->client = new OpenStack($params['url'], $params);
$cacheKey = $this->params['username'] . '@' . $this->params['url'] . '/' . $this->params['bucket'];
}
$cacheFactory = \OC::$server->getMemCacheFactory();
$this->memcache = $cacheFactory->create('swift::' . $cacheKey);
$this->params = $params;
}
@ -82,19 +91,28 @@ class Swift implements IObjectStore {
return;
}
try {
$this->client->authenticate();
} catch (ClientErrorResponseException $e) {
$statusCode = $e->getResponse()->getStatusCode();
if ($statusCode == 412) {
throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
} else if ($statusCode === 401) {
throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
} else {
throw new StorageAuthException('Unknown error', $e);
$this->importToken();
/** @var Token $token */
$token = $this->client->getTokenObject();
if (!$token || $token->hasExpired()) {
try {
$this->client->authenticate();
$this->exportToken();
} catch (ClientErrorResponseException $e) {
$statusCode = $e->getResponse()->getStatusCode();
if ($statusCode == 412) {
throw new StorageAuthException('Precondition failed, verify the keystone url', $e);
} else if ($statusCode === 401) {
throw new StorageAuthException('Authentication failed, verify the username, password and possibly tenant', $e);
} else {
throw new StorageAuthException('Unknown error', $e);
}
}
}
/** @var Catalog $catalog */
$catalog = $this->client->getCatalog();
@ -137,6 +155,40 @@ class Swift implements IObjectStore {
}
}
private function exportToken() {
$export = $this->client->exportCredentials();
$export['catalog'] = array_map(function (CatalogItem $item) {
return [
'name' => $item->getName(),
'endpoints' => $item->getEndpoints(),
'type' => $item->getType()
];
}, $export['catalog']->getItems());
$this->memcache->set('token', json_encode($export));
}
private function importToken() {
$cachedTokenString = $this->memcache->get('token');
if ($cachedTokenString) {
$cachedToken = json_decode($cachedTokenString, true);
$cachedToken['catalog'] = array_map(function (array $item) {
$itemClass = new \stdClass();
$itemClass->name = $item['name'];
$itemClass->endpoints = array_map(function (array $endpoint) {
return (object) $endpoint;
}, $item['endpoints']);
$itemClass->type = $item['type'];
return $itemClass;
}, $cachedToken['catalog']);
try {
$this->client->importCredentials($cachedToken);
} catch (\Exception $e) {
$this->client->setTokenObject(new Token());
}
}
}
/**
* @param Catalog $catalog
* @param $name