Changeset

12425:eabcc3ae22e9

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.
author Matthew Wild <mwild1@gmail.com>
date Mon, 21 Mar 2022 11:01:58 +0000
parents 12424:929bfe92bb56
children 12426:7a3da1acace1
files net/connect.lua
diffstat 1 files changed, 11 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- 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)