From 37f124fc464dd00149b18902a8599c964120da2c Mon Sep 17 00:00:00 2001 From: selta Date: Sat, 1 Oct 2022 23:16:11 -0700 Subject: [PATCH 1/2] Adding exponential backoff for initial influx connection --- speedflux/influx.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/speedflux/influx.py b/speedflux/influx.py index cc189dc..6f853e8 100644 --- a/speedflux/influx.py +++ b/speedflux/influx.py @@ -1,4 +1,4 @@ -import sys +import sys, random, time from urllib3.exceptions import NewConnectionError from influxdb import InfluxDBClient @@ -25,7 +25,7 @@ def client(self): speedflux.LOG.debug("Client extablished") return self._client - def init_db(self): + def init_db(self, backoff_in_seconds = 1): try: speedflux.LOG.debug("Intitializing Influx Database") @@ -44,6 +44,7 @@ def init_db(self): speedflux.LOG.error( "Database Init failed for 3rd time. Exiting") sys.exit() + sleep = (backoff_in_seconds * 2 ** self.retries + random.uniform(0, 1)) self.retries += 1 speedflux.LOG.error( "Connection to influx host was refused. This likely " @@ -51,6 +52,8 @@ def init_db(self): f"incorrect. It's currently '{self.config.INFLUX_DB_ADDRESS}'") speedflux.LOG.error("Full Error follows\n") speedflux.LOG.error(bad_host) + speedflux.LOG.info(f"Backing off before retrying. Sleeping for {sleep} seconds") + time.sleep(sleep) speedflux.LOG.error(f"Retry {self.retries}: Initiliazing DB.") self.init_db() From 1c2e7dcdd9ee2b225916bfd526fd3501ca088470 Mon Sep 17 00:00:00 2001 From: selta Date: Sat, 1 Oct 2022 23:17:23 -0700 Subject: [PATCH 2/2] Making it default to 2s --- speedflux/influx.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/speedflux/influx.py b/speedflux/influx.py index 6f853e8..f7213ab 100644 --- a/speedflux/influx.py +++ b/speedflux/influx.py @@ -25,7 +25,7 @@ def client(self): speedflux.LOG.debug("Client extablished") return self._client - def init_db(self, backoff_in_seconds = 1): + def init_db(self, backoff_in_seconds = 2): try: speedflux.LOG.debug("Intitializing Influx Database")