mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-10 01:25:21 +00:00
Improve logic of checking valid cached values in lambdas
This commit is contained in:
parent
a117997d5b
commit
083be24a74
@ -219,3 +219,12 @@ def list_runners(access_token: str) -> RunnerDescriptions:
|
||||
result.append(desc)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def cached_value_is_valid(updated_at: float, ttl: float) -> bool:
|
||||
"a common function to identify if cachable value is still valid"
|
||||
if updated_at == 0:
|
||||
return False
|
||||
if time.time() - ttl < updated_at:
|
||||
return True
|
||||
return False
|
||||
|
@ -8,6 +8,8 @@ import boto3 # type: ignore
|
||||
import jwt
|
||||
import requests # type: ignore
|
||||
|
||||
from . import cached_value_is_valid
|
||||
|
||||
|
||||
def get_key_and_app_from_aws() -> Tuple[str, int]:
|
||||
secret_name = "clickhouse_github_secret_key"
|
||||
@ -68,7 +70,7 @@ def get_access_token_by_key_app(private_key: str, app_id: int) -> str:
|
||||
|
||||
@dataclass
|
||||
class CachedToken:
|
||||
time: int
|
||||
time: float
|
||||
value: str
|
||||
updating: bool = False
|
||||
|
||||
@ -81,12 +83,9 @@ def get_cached_access_token() -> str:
|
||||
return _cached_token.value
|
||||
# Indicate that the value is updating now, so the cached value can be
|
||||
# used. The first setting and close-to-ttl are not counted as update
|
||||
if _cached_token.time != 0 or time.time() - 590 < _cached_token.time:
|
||||
_cached_token.updating = True
|
||||
else:
|
||||
_cached_token.updating = False
|
||||
_cached_token.updating = cached_value_is_valid(_cached_token.time, 590)
|
||||
private_key, app_id = get_key_and_app_from_aws()
|
||||
_cached_token.time = int(time.time())
|
||||
_cached_token.time = time.time()
|
||||
_cached_token.value = get_access_token_by_key_app(private_key, app_id)
|
||||
_cached_token.updating = False
|
||||
return _cached_token.value
|
||||
|
@ -9,13 +9,13 @@ from typing import Any, Dict, List
|
||||
|
||||
import boto3 # type: ignore
|
||||
|
||||
from lambda_shared import RunnerDescriptions, list_runners
|
||||
from lambda_shared import RunnerDescriptions, list_runners, cached_value_is_valid
|
||||
from lambda_shared.token import get_access_token_by_key_app, get_cached_access_token
|
||||
|
||||
|
||||
@dataclass
|
||||
class CachedInstances:
|
||||
time: int
|
||||
time: float
|
||||
value: dict
|
||||
updating: bool = False
|
||||
|
||||
@ -27,17 +27,12 @@ def get_cached_instances() -> dict:
|
||||
"""return cached instances description with updating it once per five minutes"""
|
||||
if time.time() - 250 < cached_instances.time or cached_instances.updating:
|
||||
return cached_instances.value
|
||||
# Indicate that the value is updating now, so the cached value can be
|
||||
# used. The first setting and close-to-ttl are not counted as update
|
||||
if cached_instances.time != 0 or time.time() - 300 < cached_instances.time:
|
||||
cached_instances.updating = True
|
||||
else:
|
||||
cached_instances.updating = False
|
||||
cached_instances.updating = cached_value_is_valid(cached_instances.time, 300)
|
||||
ec2_client = boto3.client("ec2")
|
||||
instances_response = ec2_client.describe_instances(
|
||||
Filters=[{"Name": "instance-state-name", "Values": ["running"]}]
|
||||
)
|
||||
cached_instances.time = int(time.time())
|
||||
cached_instances.time = time.time()
|
||||
cached_instances.value = {
|
||||
instance["InstanceId"]: instance
|
||||
for reservation in instances_response["Reservations"]
|
||||
|
Loading…
Reference in New Issue
Block a user