|
| 1 | +from dataclasses import dataclass, field |
| 2 | +from enum import Enum, auto |
| 3 | +from typing import Callable, Protocol |
| 4 | + |
| 5 | +from aiodynamo.errors import AIODynamoError |
| 6 | + |
| 7 | + |
| 8 | +class HealthMonitor(Protocol): |
| 9 | + """ |
| 10 | + Protocol for health monitors that can be configured on clients. |
| 11 | +
|
| 12 | + These can be used to detect unhealthy instances on AWS that can no longer communicate with DynamoDB. |
| 13 | + """ |
| 14 | + |
| 15 | + def is_healthy(self) -> bool: |
| 16 | + """ |
| 17 | + Returns whether the client should be considered healthy or not. |
| 18 | + """ |
| 19 | + |
| 20 | + def on_exception(self, exc: Exception) -> None: |
| 21 | + """ |
| 22 | + A request sent to DynamoDB resulted in an exception. |
| 23 | + """ |
| 24 | + |
| 25 | + def on_success(self) -> None: |
| 26 | + """ |
| 27 | + A request sent to DynamoDB succeeded. |
| 28 | + """ |
| 29 | + |
| 30 | + |
| 31 | +class OnSuccess(Enum): |
| 32 | + decrement = auto() |
| 33 | + reset = auto() |
| 34 | + noop = auto() |
| 35 | + |
| 36 | + |
| 37 | +@dataclass(kw_only=True) |
| 38 | +class CountingHealthMonitor: |
| 39 | + """ |
| 40 | + A simple health monitor that counts any exception (other than those defined in `ignore_exceptions`). |
| 41 | + Your system should monitor the `healthy` property to see if this client is considered healthy. |
| 42 | + Adjust the number of failures that need to happen before it gets marked as unhealthy using the |
| 43 | + `max_failures` property. |
| 44 | + `on_success_action` controls what should happen when a successful request finished. |
| 45 | + """ |
| 46 | + |
| 47 | + max_failures: int = 5 |
| 48 | + ignore_exceptions: tuple[type[Exception]] = (AIODynamoError,) |
| 49 | + on_success_action: OnSuccess = OnSuccess.decrement |
| 50 | + _count: int = field(init=False, default=0) |
| 51 | + |
| 52 | + def is_healthy(self) -> bool: |
| 53 | + return self._count < self.max_failures |
| 54 | + |
| 55 | + def on_exception(self, exc: Exception) -> None: |
| 56 | + if isinstance(exc, self.ignore_exceptions): |
| 57 | + return |
| 58 | + self._count += 1 |
| 59 | + |
| 60 | + def on_success(self) -> None: |
| 61 | + match self.on_success_action: |
| 62 | + case OnSuccess.decrement: |
| 63 | + self._count = max(self._count - 1, 0) |
| 64 | + case OnSuccess.reset: |
| 65 | + self._count = 0 |
| 66 | + case OnSuccess.noop: |
| 67 | + pass |
| 68 | + |
| 69 | + |
| 70 | +class CallStrategy(Enum): |
| 71 | + edge = auto() |
| 72 | + always = auto() |
| 73 | + once = auto() |
| 74 | + |
| 75 | + |
| 76 | +@dataclass |
| 77 | +class CallbackHealthMonitor: |
| 78 | + """ |
| 79 | + A health monitor wrapping another health monitor and calling the provided callback according to the |
| 80 | + `callback_strategy`. The callback takes no arguments and its return value is ignored. |
| 81 | + """ |
| 82 | + |
| 83 | + inner: HealthMonitor |
| 84 | + callback: Callable[[], None] |
| 85 | + call_strategy: CallStrategy = CallStrategy.edge |
| 86 | + _was_healthy: bool = field(init=False, default=True) |
| 87 | + _did_call: bool = field(init=False, default=False) |
| 88 | + |
| 89 | + def is_healthy(self) -> bool: |
| 90 | + return self.inner.is_healthy() |
| 91 | + |
| 92 | + def _call(self) -> None: |
| 93 | + self.callback() |
| 94 | + self._did_call = True |
| 95 | + |
| 96 | + def on_exception(self, exc: Exception) -> None: |
| 97 | + self.inner.on_exception(exc) |
| 98 | + healthy = self.is_healthy() |
| 99 | + if not healthy: |
| 100 | + match self.call_strategy: |
| 101 | + case CallStrategy.edge: |
| 102 | + if self._was_healthy: |
| 103 | + self._call() |
| 104 | + case CallStrategy.always: |
| 105 | + self._call() |
| 106 | + case CallStrategy.once: |
| 107 | + if not self._did_call: |
| 108 | + self._call() |
| 109 | + self._was_healthy = healthy |
| 110 | + |
| 111 | + def on_success(self) -> None: |
| 112 | + self.inner.on_success() |
| 113 | + self._was_healthy = self.is_healthy() |
| 114 | + |
| 115 | + |
| 116 | +class NoOpHealthMonitor: |
| 117 | + """ |
| 118 | + Noop health monitor. Doesn't do anything and always reports as healthy. |
| 119 | + """ |
| 120 | + |
| 121 | + def is_healthy(self) -> bool: |
| 122 | + return True |
| 123 | + |
| 124 | + def on_exception(self, exc: Exception) -> None: |
| 125 | + pass |
| 126 | + |
| 127 | + def on_success(self) -> None: |
| 128 | + pass |
0 commit comments