diff options
| -rw-r--r-- | bot/bot.py | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/bot/bot.py b/bot/bot.py index eee940637..b097513f1 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -37,6 +37,7 @@ class Bot(commands.Bot): self._connector = None self._resolver = None + self._statsd_timerhandle: asyncio.TimerHandle = None self._guild_available = asyncio.Event() statsd_url = constants.Stats.statsd_host @@ -48,20 +49,24 @@ class Bot(commands.Bot): statsd_url = LOCALHOST self.stats = AsyncStatsClient(self.loop, LOCALHOST) - self.connect_statsd(statsd_url) + self._connect_statsd(statsd_url) - def connect_statsd(self, statsd_url: str, retry_after: int = 2, attempt: int = 1) -> None: + def _connect_statsd(self, statsd_url: str, retry_after: int = 2, attempt: int = 1) -> None: """Callback used to retry a connection to statsd if it should fail.""" - if attempt > 5: - log.error("Reached 10 attempts trying to reconnect AsyncStatsClient. Aborting") + if self._statsd_timerhandle and not self._statsd_timerhandle.cancelled: + self._statsd_timerhandle.cancel() + + if attempt >= 5: + log.error("Reached 5 attempts trying to reconnect AsyncStatsClient. Aborting") return try: self.stats = AsyncStatsClient(self.loop, statsd_url, 8125, prefix="bot") except socket.gaierror: - log.warning(f"Statsd client failed to connect (Attempts: {attempt})") + log.warning(f"Statsd client failed to connect (Attempt(s): {attempt})") # Use a fallback strategy for retrying, up to 5 times. - self.loop.call_later(retry_after, self.retry_statsd_connection, statsd_url, retry_after ** 2, attempt + 1) + self._statsd_timerhandle = self.loop.call_later( + retry_after, self._connect_statsd, statsd_url, retry_after * 5, attempt + 1) async def cache_filter_list_data(self) -> None: """Cache all the data in the FilterList on the site.""" @@ -167,6 +172,9 @@ class Bot(commands.Bot): if self.redis_session: await self.redis_session.close() + if self._statsd_timerhandle and not self._statsd_timerhandle.cancelled: + self._statsd_timerhandle.cancel() + def insert_item_into_filter_list_cache(self, item: Dict[str, str]) -> None: """Add an item to the bots filter_list_cache.""" type_ = item["type"] |