Skip to content

Commit

Permalink
Better connection timeout message
Browse files Browse the repository at this point in the history
  • Loading branch information
danijar committed Aug 7, 2024
1 parent 0eee093 commit 92fbc11
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
10 changes: 5 additions & 5 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def test_multiple_clients(self, Server, addr):
with server:
clients = []
for i in range(10):
client = zerofun.Client(addr, i, pings=0, maxage=1)
client = zerofun.Client(addr, str(i), pings=0, maxage=1)
client.connect()
clients.append(client)
futures = [
Expand Down Expand Up @@ -95,7 +95,7 @@ def test_future_order(self, Server, addr):
server = Server(addr)
server.bind('function', lambda data: data)
with server:
client = zerofun.Client(addr, 0, pings=0, maxage=1)
client = zerofun.Client(addr, pings=0, maxage=1)
client.connect(retry=False, timeout=1)
future1 = client.function({'foo': 1})
future2 = client.function({'foo': 2})
Expand All @@ -112,7 +112,7 @@ def test_future_cleanup(self, Server, addr):
server.bind('function', lambda data: data)
with server:
client = zerofun.Client(
addr, 0, pings=0, maxage=1, maxinflight=None, errors=False)
addr, pings=0, maxage=1, maxinflight=None, errors=False)
client.connect(retry=False, timeout=1)
client.function({'foo': 1})
client.function({'foo': 2})
Expand Down Expand Up @@ -141,7 +141,7 @@ def workfn(data):

with server:
client = zerofun.Client(
addr, 0, pings=0, maxage=1, maxinflight=2)
addr, pings=0, maxage=1, maxinflight=2)
client.connect(retry=False, timeout=1)
futures = [client.function({'foo': i}) for i in range(4)]
results = [future.result()['foo'] for future in futures]
Expand All @@ -154,7 +154,7 @@ def test_future_cleanup_errors(self, Server, addr):
server = Server(addr)
server.bind('function', lambda data: data)
with server:
client = zerofun.Client(addr, 0, pings=0, maxage=1, errors=True)
client = zerofun.Client(addr, pings=0, maxage=1, errors=True)
client.connect(retry=False, timeout=1)
client.function({'foo': 1})
client.function({'foo': 2})
Expand Down
13 changes: 10 additions & 3 deletions zerofun/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def __init__(
self, address, name='Client', ipv6=False, identity=None,
pings=10, maxage=120, maxinflight=16, errors=True,
connect=False):
assert isinstance(name, str)
if identity is None:
identity = int(np.random.randint(2 ** 32))
self.address = address
Expand Down Expand Up @@ -52,9 +53,10 @@ def stats(self):

@elements.timer.section('client_connect')
def connect(self, retry=True, timeout=10):
self._print(f'Connecting to {self.resolved}')
warned = False
while True:
self.resolved = self._resolve(self.address)
self._print(f'Connecting to {self.resolved}')
try:
self.socket.connect(self.resolved, timeout)
self._print('Connection established')
Expand All @@ -63,7 +65,12 @@ def connect(self, retry=True, timeout=10):
except sockets.ProtocolError as e:
self._print(f'Ignoring unexpected message: {e}')
except sockets.ConnectError:
pass
if not warned:
self._print(
'\n +------------------------------------------+' +
'\n | Could not connect yet; retrying forever. |' +
'\n +------------------------------------------+')
warned = True
if retry:
continue
else:
Expand Down Expand Up @@ -99,7 +106,7 @@ def close(self):
return self.socket.close()

def _receive(self, rid, retry):
with elements.timer.section(f'client_{self.name.lower()}_receive'):
with elements.timer.section(f'{self.name}_client_receive'):
while rid in self.futures and not self.futures[rid].done():
result = self._listen()
if result is None and not retry:
Expand Down

0 comments on commit 92fbc11

Please sign in to comment.