-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add caching of ACM certificate lookups (#180)
- Loading branch information
1 parent
996687a
commit d400410
Showing
6 changed files
with
80 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import datetime | ||
import logging | ||
import pathlib | ||
import time | ||
import functools | ||
import shelve | ||
import shutil | ||
from contextlib import contextmanager | ||
from collections import defaultdict | ||
|
||
from aladdin.lib.cluster_rules import ClusterRules | ||
|
||
cache_root = pathlib.Path.home() / ".aladdin" / "cache" | ||
|
||
|
||
@contextmanager | ||
def clear_on_error(): | ||
try: | ||
yield | ||
except Exception: | ||
logging.info("Clearing cache due to error") | ||
shutil.rmtree(cache_root, ignore_errors=True) | ||
|
||
|
||
def certificate_cache(func): | ||
cache_root.mkdir(parents=True, exist_ok=True) | ||
cache_path = cache_root / "certificates" | ||
ttls = defaultdict( | ||
# the default ttl for existing certificates | ||
lambda: datetime.timedelta(hours=1), | ||
# allow checking the status more frequently for new certificates | ||
{ | ||
"": datetime.timedelta(minutes=1), | ||
None: datetime.timedelta(minutes=1), | ||
}, | ||
) | ||
|
||
@functools.wraps(func) | ||
@clear_on_error() | ||
def wrapper(certificate_scope): | ||
cache = shelve.open(cache_path) | ||
data: dict = cache.get(certificate_scope) or {} | ||
|
||
age = time.time() - data.get("time", 0) | ||
value = data.get("value") | ||
ttl = ttls[value] | ||
if ( | ||
not data | ||
or age > ttl.total_seconds() | ||
or not ClusterRules().certificate_lookup_cache | ||
): | ||
value = func(certificate_scope) | ||
cache[certificate_scope] = { | ||
"value": value, | ||
"time": time.time(), | ||
} | ||
cache.close() | ||
elif value: | ||
logging.info( | ||
"Found CACHED certificate %s for %s", value, certificate_scope | ||
) | ||
return value | ||
|
||
return wrapper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters