From b42fe8746c442351e0330c4570b0ae7eba102fe8 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Mon, 21 Mar 2022 11:01:58 +0000 Subject: net.connect: Improve handling of failure when attempts are still pending This could lead to failure being reported too early, even if some connections have not yet failed. --- net/connect.lua | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/net/connect.lua b/net/connect.lua index 241cc65b..0ae9c2b2 100644 --- a/net/connect.lua +++ b/net/connect.lua @@ -33,8 +33,13 @@ local function attempt_connection(p) if not conn_type then -- No more targets to try p:log("debug", "No more connection targets to try", p.target_resolver.last_error); - if p.listeners.onfail then - p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service"); + if next(p.conns) == nil then + p:log("debug", "No more targets, no pending connections. Connection failed."); + if p.listeners.onfail then + p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service"); + end + else + p:log("debug", "One or more connection attempts are still pending. Waiting for now."); end return; end @@ -88,7 +93,10 @@ function pending_connection_listeners.ondisconnect(conn, reason) p.conns[conn] = nil; p.last_error = reason or "unknown reason"; p:log("debug", "Connection attempt failed: %s", p.last_error); - attempt_connection(p); + if next(p.conns) == nil and not p.connected then + p:log("debug", "No pending connection attempts, and not yet connected"); + attempt_connection(p); + end end local function connect(target_resolver, listeners, options, data) -- cgit v1.2.3