Comparison

net/connect.lua @ 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
parent 12412:18a3a6218100
child 12426:7a3da1acace1
comparison
equal deleted inserted replaced
12424:929bfe92bb56 12425:eabcc3ae22e9
31 p:log("debug", "Checking for targets..."); 31 p:log("debug", "Checking for targets...");
32 p.target_resolver:next(function (conn_type, ip, port, extra, more_targets_available) 32 p.target_resolver:next(function (conn_type, ip, port, extra, more_targets_available)
33 if not conn_type then 33 if not conn_type then
34 -- No more targets to try 34 -- No more targets to try
35 p:log("debug", "No more connection targets to try", p.target_resolver.last_error); 35 p:log("debug", "No more connection targets to try", p.target_resolver.last_error);
36 if p.listeners.onfail then 36 if next(p.conns) == nil then
37 p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service"); 37 p:log("debug", "No more targets, no pending connections. Connection failed.");
38 if p.listeners.onfail then
39 p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service");
40 end
41 else
42 p:log("debug", "One or more connection attempts are still pending. Waiting for now.");
38 end 43 end
39 return; 44 return;
40 end 45 end
41 p:log("debug", "Next target to try is %s:%d", ip, port); 46 p:log("debug", "Next target to try is %s:%d", ip, port);
42 local conn, err = server.addclient(ip, port, pending_connection_listeners, p.options.pattern or "*a", 47 local conn, err = server.addclient(ip, port, pending_connection_listeners, p.options.pattern or "*a",
86 return; 91 return;
87 end 92 end
88 p.conns[conn] = nil; 93 p.conns[conn] = nil;
89 p.last_error = reason or "unknown reason"; 94 p.last_error = reason or "unknown reason";
90 p:log("debug", "Connection attempt failed: %s", p.last_error); 95 p:log("debug", "Connection attempt failed: %s", p.last_error);
91 attempt_connection(p); 96 if next(p.conns) == nil and not p.connected then
97 p:log("debug", "No pending connection attempts, and not yet connected");
98 attempt_connection(p);
99 end
92 end 100 end
93 101
94 local function connect(target_resolver, listeners, options, data) 102 local function connect(target_resolver, listeners, options, data)
95 local p = setmetatable({ 103 local p = setmetatable({
96 id = new_id(); 104 id = new_id();