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

Add null logger #11

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
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
10 changes: 5 additions & 5 deletions src/pgcachewatch/decorators.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)
Expand All @@ -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")

Expand Down
12 changes: 6 additions & 6 deletions src/pgcachewatch/listeners.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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())
5 changes: 5 additions & 0 deletions src/pgcachewatch/logconfig.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import logging
from typing import Final

logger: Final = logging.getLogger("pgcachewatch")
logger.addHandler(logging.NullHandler())
8 changes: 3 additions & 5 deletions src/pgcachewatch/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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

Expand Down
Loading