piker/piker/_cacheables.py

198 lines
5.1 KiB
Python
Raw Normal View History

2020-11-06 17:23:14 +00:00
# piker: trading gear for hackers
2021-02-21 17:32:40 +00:00
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
2020-11-06 17:23:14 +00:00
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2020-08-21 18:28:02 +00:00
"""
2021-08-09 15:43:45 +00:00
Cacheing apis and toolz.
2021-02-21 17:32:40 +00:00
2020-08-21 18:28:02 +00:00
"""
2021-08-10 12:51:03 +00:00
# further examples of interest:
# https://gist.github.com/njsmith/cf6fc0a97f53865f2c671659c88c1798#file-cache-py-L8
from collections import OrderedDict
2021-08-10 13:32:59 +00:00
from typing import (
Any,
2021-08-10 13:32:59 +00:00
Hashable,
Optional,
2021-08-10 13:32:59 +00:00
TypeVar,
AsyncContextManager,
)
from contextlib import (
asynccontextmanager,
)
2020-08-21 18:28:02 +00:00
import trio
from trio_typing import TaskStatus
import tractor
2020-08-21 18:28:02 +00:00
2021-08-09 15:43:45 +00:00
from .brokers import get_brokermod
from .log import get_logger
2020-08-21 18:28:02 +00:00
2021-08-09 23:27:42 +00:00
T = TypeVar('T')
2020-08-21 18:28:02 +00:00
log = get_logger(__name__)
def async_lifo_cache(maxsize=128):
"""Async ``cache`` with a LIFO policy.
Implemented my own since no one else seems to have
a standard. I'll wait for the smarter people to come
up with one, but until then...
"""
cache = OrderedDict()
def decorator(fn):
async def wrapper(*args):
key = args
try:
return cache[key]
except KeyError:
if len(cache) >= maxsize:
# discard last added new entry
cache.popitem()
# do it
cache[key] = await fn(*args)
return cache[key]
return wrapper
return decorator
_cache: dict[str, 'Client'] = {} # noqa
2020-08-21 18:28:02 +00:00
class cache:
'''Globally (processs wide) cached, task access to a
kept-alive-while-in-use async resource.
'''
lock = trio.Lock()
users: int = 0
2021-09-01 13:45:14 +00:00
values: dict[Any, Any] = {}
resources: dict[
int,
Optional[tuple[trio.Nursery, trio.Event]]
] = {}
no_more_users: Optional[trio.Event] = None
@classmethod
async def run_ctx(
cls,
mng,
key,
task_status: TaskStatus[T] = trio.TASK_STATUS_IGNORED,
) -> None:
async with mng as value:
2021-09-01 13:45:14 +00:00
_, no_more_users = cls.resources[id(mng)]
cls.values[key] = value
task_status.started(value)
try:
2021-09-01 13:45:14 +00:00
await no_more_users.wait()
finally:
value = cls.values.pop(key)
# discard nursery ref so it won't be re-used (an error)
2021-09-01 13:45:14 +00:00
cls.resources.pop(id(mng))
@asynccontextmanager
2021-08-09 23:27:42 +00:00
async def maybe_open_ctx(
2021-08-09 23:27:42 +00:00
key: Hashable,
mngr: AsyncContextManager[T],
) -> (bool, T):
2021-08-09 23:27:42 +00:00
'''Maybe open a context manager if there is not already a cached
version for the provided ``key``. Return the cached instance on
a cache hit.
2021-08-09 23:27:42 +00:00
'''
await cache.lock.acquire()
ctx_key = id(mngr)
value = None
try:
# lock feed acquisition around task racing / ``trio``'s
# scheduler protocol
value = cache.values[key]
2021-09-01 13:45:14 +00:00
log.info(f'Reusing cached resource for {key}')
cache.users += 1
cache.lock.release()
yield True, value
except KeyError:
log.info(f'Allocating new feed for {key}')
# **critical section** that should prevent other tasks from
# checking the cache until complete otherwise the scheduler
# may switch and by accident we create more then one feed.
2021-09-01 13:45:14 +00:00
# TODO: avoid pulling from ``tractor`` internals and
# instead offer a "root nursery" in piker actors?
service_n = tractor.current_actor()._service_n
# TODO: does this need to be a tractor "root nursery"?
2021-09-01 13:45:14 +00:00
ln = cache.resources.get(ctx_key)
assert not ln
2021-09-01 13:45:14 +00:00
ln, _ = cache.resources[ctx_key] = (service_n, trio.Event())
value = await ln.start(cache.run_ctx, mngr, key)
cache.users += 1
cache.lock.release()
yield False, value
finally:
cache.users -= 1
if cache.lock.locked():
cache.lock.release()
if value is not None:
# if no more consumers, teardown the client
if cache.users <= 0:
2021-09-01 13:45:14 +00:00
log.warning(f'De-allocating resource for {key}')
# terminate mngr nursery
2021-09-02 14:09:09 +00:00
entry = cache.resources.get(ctx_key)
if entry:
_, no_more_users = entry
no_more_users.set()
@asynccontextmanager
async def open_cached_client(
brokername: str,
) -> 'Client': # noqa
'''Get a cached broker client from the current actor's local vars.
If one has not been setup do it and cache it.
'''
brokermod = get_brokermod(brokername)
async with maybe_open_ctx(
key=brokername,
mngr=brokermod.get_client(),
) as (cache_hit, client):
yield client