Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
fix exponential backoff not working
Browse files Browse the repository at this point in the history
  • Loading branch information
mjmeli committed Dec 16, 2021
1 parent e49672c commit cd407e2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/pyduke_energy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
MESSAGE_TIMEOUT_RETRY_COUNT = 3

# in minutes, minimum amount of time to wait before retrying connection on forever loop
FOREVER_RETRY_BASE_MIN_MINUTES = 1
FOREVER_RETRY_MIN_MINUTES = 1

# in minutes, maximum amount of time to wait before trying connection on forever loop
FOREVER_RETRY_BASE_MAX_MINUTES = 60
FOREVER_RETRY_MAX_MINUTES = 60

# in seconds, how long to wait for a connection before timing out
CONNECT_TIMEOUT_SEC = 60
11 changes: 5 additions & 6 deletions src/pyduke_energy/realtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import functools
import json
import logging
import math
import socket
import ssl
import time
Expand All @@ -16,8 +15,8 @@
from pyduke_energy.const import (
CONNECT_TIMEOUT_SEC,
FASTPOLL_TIMEOUT_SEC,
FOREVER_RETRY_BASE_MAX_MINUTES,
FOREVER_RETRY_BASE_MIN_MINUTES,
FOREVER_RETRY_MAX_MINUTES,
FOREVER_RETRY_MIN_MINUTES,
MESSAGE_TIMEOUT_RETRY_COUNT,
MESSAGE_TIMEOUT_SEC,
MQTT_ENDPOINT,
Expand Down Expand Up @@ -227,8 +226,8 @@ async def connect_and_subscribe_forever(self):
# Exponential backoff of retry interval, maxing out at FOREVER_RETRY_BASE_MAX_MINUTES
self._forever_retry_count += 1
reconnect_interval = min(
math.pow(FOREVER_RETRY_BASE_MIN_MINUTES, self._forever_retry_count),
FOREVER_RETRY_BASE_MAX_MINUTES,
FOREVER_RETRY_MIN_MINUTES * 2 ** (self._forever_retry_count - 1),
FOREVER_RETRY_MAX_MINUTES,
)
_LOGGER.warning(
"Caught retryable error '%s' in forever loop. Will attempt reconnect in %d minute(s). Attempt #%d. Error: %s'",
Expand All @@ -237,7 +236,7 @@ async def connect_and_subscribe_forever(self):
self._forever_retry_count,
retry_err,
)
await asyncio.sleep(reconnect_interval)
await asyncio.sleep(reconnect_interval * 60) # interval is in minutes
except Exception as error:
_LOGGER.error(
"Caught non-retryable error '%s' in forever loop. Will not attempt reconnect. Error: %s",
Expand Down

0 comments on commit cd407e2

Please sign in to comment.