tahoe-lafs/src/allmydata/util/connection_status.py
Brian Warner da4e7dcfbe hush a bunch of not-really-problems caught by the lgtm.com static analyzer
This is all minor stuff: unreachable debug code (that should be commented-out
instead of in an 'if False:' block), unnecessary 'pass' and 'global'
statements, redundantly-initialized variables. No behavior changes. Nothing
here was actually broken, it just looked suspicious to the static analysis at
https://lgtm.com/projects/g/tahoe-lafs/tahoe-lafs/alerts/?mode=list .
2017-12-31 00:19:41 +01:00

62 lines
2.3 KiB
Python

import time
from zope.interface import implementer
from ..interfaces import IConnectionStatus
@implementer(IConnectionStatus)
class ConnectionStatus(object):
def __init__(self, connected, summary, non_connected_statuses,
last_connection_time, last_received_time):
self.connected = connected
self.summary = summary
self.non_connected_statuses = non_connected_statuses
self.last_connection_time = last_connection_time
self.last_received_time = last_received_time
def _hint_statuses(which, handlers, statuses):
non_connected_statuses = {}
for hint in which:
handler = handlers.get(hint)
handler_dsc = " via %s" % handler if handler else ""
dsc = statuses[hint]
non_connected_statuses["%s%s" % (hint, handler_dsc)] = dsc
return non_connected_statuses
def from_foolscap_reconnector(rc, last_received):
ri = rc.getReconnectionInfo()
state = ri.state
# the Reconnector shouldn't even be exposed until it is started, so we
# should never see "unstarted"
assert state in ("connected", "connecting", "waiting"), state
ci = ri.connectionInfo
connected = False
last_connected = None
others = set(ci.connectorStatuses.keys())
summary = None
if state == "connected":
connected = True
if ci.winningHint:
others.remove(ci.winningHint)
summary = "Connected to %s via %s" % (
ci.winningHint, ci.connectionHandlers[ci.winningHint])
else:
summary = "Connected via listener (%s)" % ci.listenerStatus[0]
last_connected = ci.establishedAt
elif state == "connecting":
# ci describes the current in-progress attempt
summary = "Trying to connect"
elif state == "waiting":
now = time.time()
elapsed = now - ri.lastAttempt
delay = ri.nextAttempt - now
summary = "Reconnecting in %d seconds (last attempt %ds ago)" % \
(delay, elapsed)
# ci describes the previous (failed) attempt
non_connected_statuses = _hint_statuses(others,
ci.connectionHandlers,
ci.connectorStatuses)
cs = ConnectionStatus(connected, summary, non_connected_statuses,
last_connected, last_received)
return cs