Agent: Make request_cache() decorator thread-safe

This commit is contained in:
Mike Salvatore 2022-02-17 14:40:07 -05:00
parent 4005ea2924
commit e2d116fdf1
1 changed files with 6 additions and 3 deletions

View File

@ -1,3 +1,4 @@
import threading
from functools import wraps
from .timer import Timer
@ -29,14 +30,16 @@ def request_cache(ttl: float):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
if wrapper.timer.is_expired():
wrapper.cached_value = fn(*args, **kwargs)
wrapper.timer.set(ttl)
with wrapper.lock:
if wrapper.timer.is_expired():
wrapper.cached_value = fn(*args, **kwargs)
wrapper.timer.set(ttl)
return wrapper.cached_value
wrapper.cached_value = None
wrapper.timer = Timer()
wrapper.lock = threading.Lock()
return wrapper