summaryrefslogtreecommitdiff
path: root/zuul/web/__init__.py
diff options
context:
space:
mode:
authorJames E. Blair <jim@acmegating.com>2022-06-27 14:14:56 -0700
committerJames E. Blair <jim@acmegating.com>2022-06-27 14:14:56 -0700
commit1fd242882a233bf91080269b760c8cb583ceec63 (patch)
tree9f47f1e90c947a0bf0cf6e9cb577a29fdc6252ad /zuul/web/__init__.py
parent1bd6a1e92fb6f8f251854a230eed37af5afe72bf (diff)
downloadzuul-1fd242882a233bf91080269b760c8cb583ceec63.tar.gz
Use attributes instead of a nested dict for web cache
This is a code-style only change which uses instance attributes instead of a nested dict to store cache information in zuul-web. This is slightly shorter, easier to read/type, less subject to typos, and allows static analysis (eg pyflakes) to work better. It also was slighly misleading to have the two caches combined into a single variable since they operated very differently (one is a single cache, the other is a set of caches). Change-Id: I29ece7e8a39992724596d2d252ad7b023c50f8a8
Diffstat (limited to 'zuul/web/__init__.py')
-rwxr-xr-xzuul/web/__init__.py52
1 files changed, 23 insertions, 29 deletions
diff --git a/zuul/web/__init__.py b/zuul/web/__init__.py
index 1fa36c98a..0323dce4c 100755
--- a/zuul/web/__init__.py
+++ b/zuul/web/__init__.py
@@ -360,18 +360,13 @@ class ZuulWebAPI(object):
self.system = ZuulSystem(self.zk_client)
self.zk_nodepool = ZooKeeperNodepool(self.zk_client,
enable_node_cache=True)
- self.caches = {
- 'status': {
- 'cache': {},
- 'cache_time': {},
- 'lock': defaultdict(threading.Lock)
- },
- 'tenants': {
- 'cache': [],
- 'cache_time': 0,
- 'lock': threading.Lock()
- },
- }
+ self.status_caches = {}
+ self.status_cache_times = {}
+ self.status_cache_locks = defaultdict(threading.Lock)
+ self.tenants_cache = []
+ self.tenants_cache_time = 0
+ self.tenants_cache_lock = threading.Lock()
+
self.cache_expiry = 1
self.static_cache_expiry = zuulweb.static_cache_expiry
@@ -971,21 +966,21 @@ class ZuulWebAPI(object):
@cherrypy.expose
@cherrypy.tools.json_out(content_type='application/json; charset=utf-8')
def tenants(self):
- cache_time = self.caches['tenants']['cache_time']
+ cache_time = self.tenants_cache_time
if time.time() - cache_time > self.cache_expiry:
- with self.caches['tenants']['lock']:
- self.caches['tenants']['cache'] = self._tenants()
- self.caches['tenants']['cache_time'] = time.time()
+ with self.tenants_cache_lock:
+ self.tenants_cache = self._tenants()
+ self.tenants_cache_time = time.time()
resp = cherrypy.response
resp.headers["Cache-Control"] = f"public, max-age={self.cache_expiry}"
last_modified = datetime.utcfromtimestamp(
- self.caches['tenants']['cache_time']
+ self.tenants_cache_time
)
last_modified_header = last_modified.strftime('%a, %d %b %Y %X GMT')
resp.headers["Last-modified"] = last_modified_header
resp.headers['Access-Control-Allow-Origin'] = '*'
- return self.caches['tenants']['cache']
+ return self.tenants_cache
@cherrypy.expose
@cherrypy.tools.json_out(content_type='application/json; charset=utf-8')
@@ -1014,32 +1009,31 @@ class ZuulWebAPI(object):
def _getStatus(self, tenant_name):
tenant = self._getTenantOrRaise(tenant_name)
- cache_times = self.caches['status']['cache_time']
- cache_time = cache_times.get(tenant_name, 0)
- if tenant_name not in self.caches['status']['lock'] or \
+ cache_time = self.status_cache_times.get(tenant_name, 0)
+ if tenant_name not in self.status_cache_locks or \
(time.time() - cache_time) > self.cache_expiry:
- if self.caches['status']['lock'][tenant_name].acquire(
+ if self.status_cache_locks[tenant_name].acquire(
blocking=False
):
try:
- self.caches['status']['cache'][tenant_name] =\
+ self.status_caches[tenant_name] =\
self.formatStatus(tenant)
- self.caches['status']['cache_time'][tenant_name] =\
+ self.status_cache_times[tenant_name] =\
time.time()
finally:
- self.caches['status']['lock'][tenant_name].release()
- if not self.caches['status']['cache'].get(tenant_name):
+ self.status_cache_locks[tenant_name].release()
+ if not self.status_caches.get(tenant_name):
# If the cache is empty at this point it means that we didn't
# get the lock but another thread is initializing the cache
# for the first time. In this case we just wait for the lock
# to wait for it to finish.
- with self.caches['status']['lock'][tenant_name]:
+ with self.status_cache_locks[tenant_name]:
pass
- payload = self.caches['status']['cache'][tenant_name]
+ payload = self.status_caches[tenant_name]
resp = cherrypy.response
resp.headers["Cache-Control"] = f"public, max-age={self.cache_expiry}"
last_modified = datetime.utcfromtimestamp(
- self.caches['status']['cache_time'][tenant_name]
+ self.status_cache_times[tenant_name]
)
last_modified_header = last_modified.strftime('%a, %d %b %Y %X GMT')
resp.headers["Last-modified"] = last_modified_header