+def connect_client(luser, service):
+ # Try multiple times to connect, with an exponential backoff
+ for retry in range(5):
+ try:
+ # Skipping the retry=0 case gives an immediate first attempt
+ if retry:
+ time.sleep((2 ** retry) / 10)
+ luser.open("::1", 4000)
+ # Attempt to poll the connection, closing if unusable
+ try:
+ luser.fill_rawq()
+ except ConnectionResetError:
+ luser.close()
+ continue
+ # Short-circuit if we get this far, connection is safe to use
+ return luser
+ except ConnectionRefusedError:
+ continue
+ else:
+ # Connection retries have been exhausted, so give up
+ tlog("\nERROR: Client could not connect.\n")
+ stop_service(service)
+ sys.exit(1)
+
+