Forgot some locks. Sigh.

This commit is contained in:
Reid 'arrdem' McKenzie 2021-12-12 21:51:23 -07:00
parent 37435f6423
commit df653a1819

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,43 +53,46 @@ class HostState(object):
self.append(resp) self.append(resp)
def append(self, resp): def append(self, resp):
if resp and not self._is_up: with self._lock:
# log.debug(f"Host {self._hostname} is up!") if resp and not self._is_up:
self._is_up = self._is_up or resp # log.debug(f"Host {self._hostname} is up!")
self._up = resp._time self._is_up = self._is_up or resp
self._up = resp._time
elif resp and self._is_up: elif resp and self._is_up:
# log.debug(f"Host {self._hostname} holding up...") # log.debug(f"Host {self._hostname} holding up...")
pass pass
elif not resp and self._is_up: elif not resp and self._is_up:
# log.debug(f"Host {self._hostname} is down!") # log.debug(f"Host {self._hostname} is down!")
self._is_up = None self._is_up = None
self._up = None self._up = None
elif not resp and not self._is_up: elif not resp and not self._is_up:
pass pass
if not resp: if not resp:
self._lost += 1 self._lost += 1
if self._state and not self._state[0]: if self._state and not self._state[0]:
self._lost -= 1 self._lost -= 1
self._state.append(resp) self._state.append(resp)
def last(self): def last(self):
return next(reversed(self._state), None) with self._lock:
return next(reversed(self._state), None)
def last_window(self, duration: timedelta = None): def last_window(self, duration: timedelta = None):
l = [] with self._lock:
t = time() - duration.total_seconds() l = []
for i in reversed(self._state): t = time() - duration.total_seconds()
if not i or i._time > t: for i in reversed(self._state):
l.insert(0, i) if not i or i._time > t:
else: l.insert(0, i)
break else:
return l break
return l
def loss(self, duration: timedelta): def loss(self, duration: timedelta):
log = self.last_window(duration) log = self.last_window(duration)
@ -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()