Skip to content
Snippets Groups Projects
Unverified Commit ba9906ae authored by Justin Welch's avatar Justin Welch Committed by GitHub
Browse files

Merge pull request #225 from steemit/dont-cache-forever

Dont cache irreversible blocks forever
parents d92287df a0899525
No related branches found
No related tags found
No related merge requests found
...@@ -33,7 +33,7 @@ from .utils import merge_cached_responses ...@@ -33,7 +33,7 @@ from .utils import merge_cached_responses
logger = structlog.getLogger(__name__) logger = structlog.getLogger(__name__)
BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.NO_EXPIRE_IF_IRREVERSIBLE]) BATCH_IRREVERSIBLE_TTL_SET = frozenset([TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE])
# types # types
CacheTTLValue = TypeVar('CacheTTL', int, float, type(None)) CacheTTLValue = TypeVar('CacheTTL', int, float, type(None))
...@@ -188,7 +188,7 @@ class CacheGroup: ...@@ -188,7 +188,7 @@ class CacheGroup:
) -> None: ) -> None:
key = jsonrpc_cache_key(request) key = jsonrpc_cache_key(request)
ttl = ttl or request.upstream.ttl ttl = ttl or request.upstream.ttl
if ttl == TTL.NO_EXPIRE_IF_IRREVERSIBLE: if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE:
last_irreversible_block_num = last_irreversible_block_num or \ last_irreversible_block_num = last_irreversible_block_num or \
self._memory_cache.gets('last_irreversible_block_num') or \ self._memory_cache.gets('last_irreversible_block_num') or \
await self.get('last_irreversible_block_num') await self.get('last_irreversible_block_num')
...@@ -218,7 +218,7 @@ class CacheGroup: ...@@ -218,7 +218,7 @@ class CacheGroup:
else: else:
new_ttls = [] new_ttls = []
for i, ttl in enumerate(ttls): for i, ttl in enumerate(ttls):
if ttl == TTL.NO_EXPIRE_IF_IRREVERSIBLE: if ttl == TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE:
ttl = irreversible_ttl(responses[i], last_irreversible_block_num) ttl = irreversible_ttl(responses[i], last_irreversible_block_num)
new_ttls.append(ttl) new_ttls.append(ttl)
triplets = filter(lambda p: p[0] != TTL.NO_CACHE, zip(ttls, requests, responses)) triplets = filter(lambda p: p[0] != TTL.NO_CACHE, zip(ttls, requests, responses))
......
...@@ -8,11 +8,11 @@ Method Settings ...@@ -8,11 +8,11 @@ Method Settings
- TTL is an integer value in seconds. Integers <= 0 have special meaning - TTL is an integer value in seconds. Integers <= 0 have special meaning
- A TTL of `0` won't expire - A TTL of `0` won't expire
- A TTL of `-1` wont be cached - A TTL of `-1` wont be cached
- A TTL of `-2` will be cached without expiration only if it is 'irreversible' in terms of blockchain consesus - A TTL of `-2` will be cached with default expiration only if it is 'irreversible' in terms of blockchain consesus
- For readabilty/writabilty, there are shorthand variables for these 'special' TTL values: - For readabilty/writabilty, there are shorthand variables for these 'special' TTL values:
- `NO_EXPIRE` == 0 - `NO_EXPIRE` == 0
- `NO_CACHE` == -1 - `NO_CACHE` == -1
- `NO_EXPIRE_IF_IRREVERSIBLE` == -2 - `DEFAULT_EXPIRE_IF_IRREVERSIBLE` == -2
""" """
...@@ -23,7 +23,7 @@ class TTL(Enum): ...@@ -23,7 +23,7 @@ class TTL(Enum):
DEFAULT_TTL = 3 DEFAULT_TTL = 3
NO_EXPIRE = None NO_EXPIRE = None
NO_CACHE = -1 NO_CACHE = -1
NO_EXPIRE_IF_IRREVERSIBLE = -2 DEFAULT_EXPIRE_IF_IRREVERSIBLE = -2
# pylint: disable=no-else-return # pylint: disable=no-else-return
def __eq__(self, other: int) -> bool: def __eq__(self, other: int) -> bool:
......
...@@ -30,8 +30,6 @@ def irreversible_ttl(jsonrpc_response: dict=None, ...@@ -30,8 +30,6 @@ def irreversible_ttl(jsonrpc_response: dict=None,
return TTL.NO_CACHE return TTL.NO_CACHE
try: try:
jrpc_block_num = block_num_from_jsonrpc_response(jsonrpc_response) jrpc_block_num = block_num_from_jsonrpc_response(jsonrpc_response)
if jrpc_block_num and jrpc_block_num <= last_irreversible_block_num:
return TTL.NO_EXPIRE
return TTL.DEFAULT_TTL return TTL.DEFAULT_TTL
except Exception as e: except Exception as e:
logger.warning( logger.warning(
......
...@@ -35,8 +35,8 @@ non_ttl_rpc_req = jsonrpc_from_request(dummy_request, 0, {"id": "1", "jsonrpc": ...@@ -35,8 +35,8 @@ non_ttl_rpc_req = jsonrpc_from_request(dummy_request, 0, {"id": "1", "jsonrpc":
(ttl_rpc_req, rpc_resp, 999, TTL.DEFAULT_TTL), (ttl_rpc_req, rpc_resp, 999, TTL.DEFAULT_TTL),
# cache when last_block_num >= response block_num # cache when last_block_num >= response block_num
(ttl_rpc_req, rpc_resp, 1000, TTL.NO_EXPIRE), (ttl_rpc_req, rpc_resp, 1000, TTL.DEFAULT_TTL),
(ttl_rpc_req, rpc_resp, 1001, TTL.NO_EXPIRE), (ttl_rpc_req, rpc_resp, 1001, TTL.DEFAULT_TTL),
# don't cache when bad/missing response block_num # don't cache when bad/missing response block_num
(ttl_rpc_req, {}, 2000, TTL.NO_CACHE), (ttl_rpc_req, {}, 2000, TTL.NO_CACHE),
...@@ -54,7 +54,7 @@ def test_ttls(rpc_req, rpc_resp, last_block_num, expected): ...@@ -54,7 +54,7 @@ def test_ttls(rpc_req, rpc_resp, last_block_num, expected):
(TTL.NO_CACHE, -1), (TTL.NO_CACHE, -1),
(TTL.DEFAULT_TTL, 3), (TTL.DEFAULT_TTL, 3),
(TTL.NO_EXPIRE, None), (TTL.NO_EXPIRE, None),
(TTL.NO_EXPIRE_IF_IRREVERSIBLE, -2), (TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE, -2),
] ]
) )
def test_ttl_eq(ttl, eq): def test_ttl_eq(ttl, eq):
...@@ -65,7 +65,7 @@ def test_ttl_eq(ttl, eq): ...@@ -65,7 +65,7 @@ def test_ttl_eq(ttl, eq):
@pytest.mark.parametrize('ttl', [ @pytest.mark.parametrize('ttl', [
(TTL.NO_CACHE), (TTL.NO_CACHE),
(TTL.DEFAULT_TTL), (TTL.DEFAULT_TTL),
(TTL.NO_EXPIRE_IF_IRREVERSIBLE) (TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE)
] ]
) )
def test_ttl_gt(ttl): def test_ttl_gt(ttl):
...@@ -75,7 +75,7 @@ def test_ttl_gt(ttl): ...@@ -75,7 +75,7 @@ def test_ttl_gt(ttl):
@pytest.mark.parametrize('ttl', [ @pytest.mark.parametrize('ttl', [
(TTL.NO_CACHE), (TTL.NO_CACHE),
(TTL.DEFAULT_TTL), (TTL.DEFAULT_TTL),
(TTL.NO_EXPIRE_IF_IRREVERSIBLE) (TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE)
] ]
) )
def test_ttl_ge(ttl): def test_ttl_ge(ttl):
...@@ -85,7 +85,7 @@ def test_ttl_ge(ttl): ...@@ -85,7 +85,7 @@ def test_ttl_ge(ttl):
@pytest.mark.parametrize('ttl', [ @pytest.mark.parametrize('ttl', [
(TTL.NO_CACHE), (TTL.NO_CACHE),
(TTL.DEFAULT_TTL), (TTL.DEFAULT_TTL),
(TTL.NO_EXPIRE_IF_IRREVERSIBLE) (TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE)
] ]
) )
def test_ttl_lt(ttl): def test_ttl_lt(ttl):
...@@ -95,7 +95,7 @@ def test_ttl_lt(ttl): ...@@ -95,7 +95,7 @@ def test_ttl_lt(ttl):
@pytest.mark.parametrize('ttl', [ @pytest.mark.parametrize('ttl', [
(TTL.NO_CACHE), (TTL.NO_CACHE),
(TTL.DEFAULT_TTL), (TTL.DEFAULT_TTL),
(TTL.NO_EXPIRE_IF_IRREVERSIBLE) (TTL.DEFAULT_EXPIRE_IF_IRREVERSIBLE)
] ]
) )
def test_ttl_le(ttl): def test_ttl_le(ttl):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment