Prevent race conditions on /healthcheck (#2273)

In a high availability deployment scenario, two clients may make a request on /healthcheck at the exact same time, which can lead to check_config returning False if the second requests changes the 'healthcheck' cache key before the first one has had time to fetch the value it had set.

A solution to counter this is to ensure different keys are used for each healthcheck.
This commit is contained in:
Smyler
2023-04-03 08:01:17 +02:00
committed by GitHub
parent 870eefb184
commit faa937020a

View File

@@ -1,4 +1,4 @@
from time import monotonic_ns
from time import time
from flask import current_app
from sqlalchemy_utils import database_exists
@@ -13,6 +13,7 @@ def check_database():
@timed_lru_cache(timeout=30)
def check_config():
value = monotonic_ns()
cache.set("healthcheck", value)
return cache.get("healthcheck") == value
key = "healthcheck"
value = round(time() / 5) * 5
cache.set(key, value)
return cache.get(key) == value