Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force reload of cred type cache periodically #722

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion server/vcr-server/agent_webhooks/utils/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
import uuid
from collections import namedtuple
from datetime import datetime
from datetime import datetime, timedelta
from importlib import import_module
import os

Expand Down Expand Up @@ -58,6 +58,8 @@
print(">>> NO not creating detail claims for credentials")
CREATE_CREDENTIAL_CLAIMS = False

CTYPE_CACHE_MAX_AGE_MINS = int(os.environ.get('CTYPE_CACHE_MAX_AGE_MINS', '10'))


def schema_key(s_id: str) -> SchemaKey:
"""
Expand Down Expand Up @@ -259,7 +261,17 @@ class CredentialManager(object):
"""

def __init__(self) -> None:
self._init_ctype_cache()

def _init_ctype_cache(self):
self._cred_type_cache = {}
self._cred_type_cache_created = datetime.now()
self._cred_type_cache_max_age = (self._cred_type_cache_created +
timedelta(minutes=CTYPE_CACHE_MAX_AGE_MINS))

def _reinit_ctype_cache(self):
if self._cred_type_cache_max_age < datetime.now():
self._init_ctype_cache()

@classmethod
def get_claims(cls, credential):
Expand Down Expand Up @@ -356,6 +368,10 @@ def get_credential_type(self, credential: (Credential, CredentialModel)):
Fetch the credential type for the incoming credential
"""
LOGGER.debug(">>> get credential context")

# force re-init to get any issuer updates
self._reinit_ctype_cache()

start_time = time.perf_counter()
result = None
type_id = getattr(credential, "credential_type_id", None)
Expand Down