Forgot some locks. Sigh.

This commit is contained in:
Reid 'arrdem' McKenzie 2021-12-12 21:51:23 -07:00
parent 7344d858c6
commit c4f13f186a

View file

@ -43,6 +43,7 @@ class HostState(object):
is_up: bool = False, is_up: bool = False,
lost: int = 0, lost: int = 0,
up: float = 0.0): up: float = 0.0):
self._lock = Lock()
self._state = ringbuffer(maxlen=history_size) self._state = ringbuffer(maxlen=history_size)
self._is_up = is_up self._is_up = is_up
self._lost = lost self._lost = lost
@ -52,6 +53,7 @@ class HostState(object):
self.append(resp) self.append(resp)
def append(self, resp): def append(self, resp):
with self._lock:
if resp and not self._is_up: if resp and not self._is_up:
# log.debug(f"Host {self._hostname} is up!") # log.debug(f"Host {self._hostname} is up!")
self._is_up = self._is_up or resp self._is_up = self._is_up or resp
@ -78,9 +80,11 @@ class HostState(object):
self._state.append(resp) self._state.append(resp)
def last(self): def last(self):
with self._lock:
return next(reversed(self._state), None) return next(reversed(self._state), None)
def last_window(self, duration: timedelta = None): def last_window(self, duration: timedelta = None):
with self._lock:
l = [] l = []
t = time() - duration.total_seconds() t = time() - duration.total_seconds()
for i in reversed(self._state): for i in reversed(self._state):
@ -140,7 +144,7 @@ def retrace(shutdown, q, opts, hl, hosts):
with hl: with hl:
if address not in hosts: if address not in hosts:
log.info(f"Monitoring {address}...") log.info(f"Monitoring {address}...")
monitor = MonitoredHost(address, timedelta(seconds=1)) monitor = MonitoredHost(address, timedelta(seconds=2))
hosts[address] = (distance, monitor) hosts[address] = (distance, monitor)
threads[address] = t = Thread(target=monitor, args=(shutdown, q)) threads[address] = t = Thread(target=monitor, args=(shutdown, q))
t.start() t.start()