Add caching of get_auth call in Swift

Should make calls significantly faster for our Swift customers

Fixes https://jira.prod.coreos.systems/browse/QS-39
This commit is contained in:
Joseph Schorr 2017-10-17 13:09:37 -04:00
parent dcec90649e
commit ffaff5a27e
2 changed files with 38 additions and 9 deletions

View file

@ -8,6 +8,8 @@ import hmac
import string
import logging
import json
from cachetools import lru_cache
from _pyio import BufferedReader
from uuid import uuid4
@ -147,6 +149,14 @@ class SwiftStorage(BaseStorage):
logger.exception('Could not head object at path %s: %s', path, ex)
return None
@lru_cache(maxsize=1)
def _get_root_storage_url(self):
""" Returns the root storage URL for this Swift storage. Note that since this requires a call
to Swift, we cache the result of this function call.
"""
storage_url, _ = self._get_connection().get_auth()
return storage_url
def get_direct_download_url(self, object_path, request_ip=None, expires_in=60, requires_cors=False, head=False):
if requires_cors:
return None
@ -155,17 +165,17 @@ class SwiftStorage(BaseStorage):
if not self._temp_url_key:
return None
# Retrieve the auth details for the connection.
# Retrieve the root storage URL for the connection.
try:
object_url_value, _ = self._get_connection().get_auth()
root_storage_url = self._get_root_storage_url()
except ClientException:
logger.exception('Got client exception when trying to load Swift auth')
return None
object_url = urlparse(object_url_value)
scheme = object_url.scheme
path = object_url.path.rstrip('/')
hostname = object_url.netloc
parsed_storage_url = urlparse(root_storage_url)
scheme = parsed_storage_url.scheme
path = parsed_storage_url.path.rstrip('/')
hostname = parsed_storage_url.netloc
object_path = self._normalize_path(object_path)