net.connect: Improve handling of failure when attempts are still pending
authorMatthew Wild <mwild1@gmail.com>
Mon, 21 Mar 2022 11:01:58 +0000
changeset 12429 eabcc3ae22e9
parent 12428 929bfe92bb56
child 12430 7a3da1acace1
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
--- a/net/connect.lua	Mon Mar 21 10:07:08 2022 +0000
+++ b/net/connect.lua	Mon Mar 21 11:01:58 2022 +0000
@@ -33,8 +33,13 @@
 		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 @@
 	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)