From 908247a6df116bb2e4c43343470469fb7d7e04cb Mon Sep 17 00:00:00 2001 From: Dan Notestein <dan@syncad.com> Date: Sat, 15 May 2021 17:22:31 +0000 Subject: [PATCH] Change function of -2 caching (previously called DEFAULT_EXPIRE_IF_IRREVRESIBLE, now called EXPIRE IF REVERSIBLE). This caching is mainly used for caching blocks and block headers. Now irreversible blocks don't have an expiration time, so they only get evicted when the cache fills up. And the default expiration time for reversible blocks is 9s (EXTENDED_TTL) --- jussi/cache/cache_group.py | 9 +++++---- jussi/cache/ttl.py | 5 +++-- jussi/cache/utils.py | 16 +++++++++++++--- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/jussi/cache/cache_group.py b/jussi/cache/cache_group.py index 83290b4..f64fcd3 100644 --- a/jussi/cache/cache_group.py +++ b/jussi/cache/cache_group.py @@ -33,7 +33,7 @@ from .utils import merge_cached_responses logger = structlog.getLogger(__name__) -BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE]) +BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.EXPIRE_IF_REVERSIBLE]) # types CacheTTLValue = TypeVar('CacheTTL', int, float, type(None)) @@ -188,13 +188,14 @@ class CacheGroup: ) -> None: key = jsonrpc_cache_key(request) ttl = ttl or request.upstream.ttl - if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE: + if ttl == TTL.EXPIRE_IF_REVERSIBLE: last_irreversible_block_num = last_irreversible_block_num or \ self._memory_cache.gets('last_irreversible_block_num') or \ await self.get('last_irreversible_block_num') ttl = irreversible_ttl(jsonrpc_response=response, - last_irreversible_block_num=last_irreversible_block_num) + last_irreversible_block_num=last_irreversible_block_num, + request=request) elif ttl == TTL.NO_CACHE: return value = self.prepare_response_for_cache(request, response) @@ -218,7 +219,7 @@ class CacheGroup: else: new_ttls = [] for i, ttl in enumerate(ttls): - if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE: + if ttl == TTL.EXPIRE_IF_REVERSIBLE: ttl = irreversible_ttl(responses[i], last_irreversible_block_num) new_ttls.append(ttl) triplets = filter(lambda p: p[0] != TTL.NO_CACHE, zip(ttls, requests, responses)) diff --git a/jussi/cache/ttl.py b/jussi/cache/ttl.py index b78a1af..d0140da 100644 --- a/jussi/cache/ttl.py +++ b/jussi/cache/ttl.py @@ -12,7 +12,7 @@ Method Settings - For readabilty/writabilty, there are shorthand variables for these 'special' TTL values: - `NO_EXPIRE` == 0 - `NO_CACHE` == -1 - - `DEFAULT_EXPIRE_IF_IRREVERSIBLE` == -2 + - `EXPIRE_IF_REVERSIBLE` == -2 """ @@ -21,9 +21,10 @@ from enum import Enum class TTL(Enum): DEFAULT_TTL = 3 + EXTENDED_TTL = 9 NO_EXPIRE = None NO_CACHE = -1 - DEFAULT_EXPIRE_IF_IRREVERSIBLE = -2 + EXPIRE_IF_REVERSIBLE = -2 # pylint: disable=no-else-return def __eq__(self, other: int) -> bool: diff --git a/jussi/cache/utils.py b/jussi/cache/utils.py index 6a3bfe2..de0e7b9 100644 --- a/jussi/cache/utils.py +++ b/jussi/cache/utils.py @@ -21,7 +21,8 @@ def jsonrpc_cache_key(single_jsonrpc_request: SingleJrpcRequest) -> str: def irreversible_ttl(jsonrpc_response: dict=None, - last_irreversible_block_num: int=None) -> TTL: + last_irreversible_block_num: int=None, + request: SingleJrpcRequest=None) -> TTL: if not jsonrpc_response: return TTL.NO_CACHE if not isinstance(last_irreversible_block_num, int): @@ -30,12 +31,21 @@ def irreversible_ttl(jsonrpc_response: dict=None, return TTL.NO_CACHE try: jrpc_block_num = block_num_from_jsonrpc_response(jsonrpc_response) - return TTL.DEFAULT_TTL + if jrpc_block_num > last_irreversible_block_num: + return TTL.EXTENDED_TTL + else: + return TTL.NO_EXPIRE except Exception as e: + if request is not None: + request_string = request.json() + else: + request_string = 'None' logger.warning( 'Unable to cache using last irreversible block', e=e, - lirb=last_irreversible_block_num) + lirb=last_irreversible_block_num, + request_string=request_string, + jsonrpc_response=jsonrpc_response) return TTL.NO_CACHE -- GitLab