forked from baby-pilot/talking-plant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
26 lines (20 loc) · 778 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from datetime import datetime
class IntervalExponentialBackOff:
"""
Implements a class for exponential backoff,
allowing each alert to be spaced out with an exponential backoff
"""
def __init__(self, base_time=10):
self._counter = 0
self._base_time = base_time
self._last_alert = datetime.min
def reset(self):
self._counter = 0
self._last_alert = datetime.min
def set(self):
self._counter += 1
self._last_alert = datetime.now()
def back_off_passed(self) -> bool:
time_since_last_alert = (datetime.now() - self._last_alert).total_seconds()
back_off_time = 5**self._counter * self._base_time # 10 seconds base time
return time_since_last_alert >= back_off_time