diff --git a/src/pgcachewatch/decorators.py b/src/pgcachewatch/decorators.py index 26754d3..5e7317a 100644 --- a/src/pgcachewatch/decorators.py +++ b/src/pgcachewatch/decorators.py @@ -1,10 +1,10 @@ import asyncio -import logging from typing import Awaitable, Callable, Hashable, Literal, TypeVar from typing_extensions import ParamSpec from pgcachewatch import strategies, utils +from pgcachewatch.logconfig import logger P = ParamSpec("P") T = TypeVar("T") @@ -20,13 +20,13 @@ def outer(fn: Callable[P, Awaitable[T]]) -> Callable[P, Awaitable[T]]: async def inner(*args: P.args, **kwargs: P.kwargs) -> T: # If db-conn is down, disable cache. if not strategy.connection_healthy(): - logging.critical("Database connection is closed, caching disabled.") + logger.critical("Database connection is closed, caching disabled.") return await fn(*args, **kwargs) # Clear cache if we have a event from # the database the instructs us to clear. if strategy.clear(): - logging.debug("Cache clear") + logger.debug("Cache clear") cached.clear() key = utils.make_key(args, kwargs) @@ -38,12 +38,12 @@ async def inner(*args: P.args, **kwargs: P.kwargs) -> T: ... else: # Cache hit - logging.debug("Cache hit") + logger.debug("Cache hit") if statistics_callback: statistics_callback("hit") return await waiter - logging.debug("Cache miss") + logger.debug("Cache miss") if statistics_callback: statistics_callback("miss") diff --git a/src/pgcachewatch/listeners.py b/src/pgcachewatch/listeners.py index 5c142f2..9a4f531 100644 --- a/src/pgcachewatch/listeners.py +++ b/src/pgcachewatch/listeners.py @@ -1,19 +1,19 @@ import asyncio import datetime import json -import logging from typing import Protocol import asyncpg from . import models +from .logconfig import logger def _critical_termination_listener(*_: object, **__: object) -> None: # Must be defined in the global namespace, as ayncpg keeps # a set of functions to call. This this will now happen once as # all instance will point to the same function. - logging.critical("Connection is closed / terminated.") + logger.critical("Connection is closed / terminated.") class EventQueueProtocol(Protocol): @@ -133,15 +133,15 @@ def parse_and_put( json.loads(payload) | {"channel": channel} ) except Exception: - logging.exception("Unable to parse `%s`.", payload) + logger.exception("Unable to parse `%s`.", payload) else: if parsed.latency > self._max_latency: - logging.warning("Latency for %s above %s.", parsed, self._max_latency) - logging.info("Received event: %s on %s", parsed, channel) + logger.warning("Latency for %s above %s.", parsed, self._max_latency) + logger.info("Received event: %s on %s", parsed, channel) try: self.put_nowait(parsed) except Exception: - logging.exception("Unable to queue `%s`.", parsed) + logger.exception("Unable to queue `%s`.", parsed) def connection_healthy(self) -> bool: return bool(self._pg_connection and not self._pg_connection.is_closed()) diff --git a/src/pgcachewatch/logconfig.py b/src/pgcachewatch/logconfig.py new file mode 100644 index 0000000..39d6b8a --- /dev/null +++ b/src/pgcachewatch/logconfig.py @@ -0,0 +1,5 @@ +import logging +from typing import Final + +logger: Final = logging.getLogger("pgcachewatch") +logger.addHandler(logging.NullHandler()) diff --git a/src/pgcachewatch/strategies.py b/src/pgcachewatch/strategies.py index 1237bb4..485acfa 100644 --- a/src/pgcachewatch/strategies.py +++ b/src/pgcachewatch/strategies.py @@ -60,9 +60,9 @@ def __init__( ) -> None: super().__init__() self._listener = listener - self._window = window self._settings = settings - self._events = collections.deque[models.OPERATIONS](maxlen=len(self._window)) + self._events = collections.deque[models.OPERATIONS](maxlen=len(window)) + self._window = collections.deque[models.OPERATIONS](window, maxlen=len(window)) def connection_healthy(self) -> bool: return self._listener.connection_healthy() @@ -73,9 +73,7 @@ def clear(self) -> bool: settings=self._settings, ): self._events.append(current.operation) - if len(self._window) == len(self._events) and all( - w == e for w, e in zip(self._window, self._events) - ): + if self._window == self._events: return True return False