# HG changeset patch # User Matthew Wild # Date 1647620161 0 # Node ID e132a427991482bbb47cf012ca72a04a9d82dfc5 # Parent 596625eed326595a18c8db00b2fa00810e6ac5dc net.connect: Support for multiple pending connection attempts diff -r 596625eed326 -r e132a4279914 net/connect.lua --- a/net/connect.lua Fri Mar 18 16:13:09 2022 +0000 +++ b/net/connect.lua Fri Mar 18 16:16:01 2022 +0000 @@ -28,10 +28,6 @@ local function attempt_connection(p) p:log("debug", "Checking for targets..."); - if p.conn then - pending_connections_map[p.conn] = nil; - p.conn = nil; - end p.target_resolver:next(function (conn_type, ip, port, extra) if not conn_type then -- No more targets to try @@ -49,7 +45,7 @@ p.last_error = err or "unknown reason"; return attempt_connection(p); end - p.conn = conn; + p.conns[conn] = true; pending_connections_map[conn] = p; end); end @@ -62,6 +58,13 @@ return; end pending_connections_map[conn] = nil; + if p.connected then + -- We already succeeded in connecting + p.conns[conn] = nil; + conn:close(); + return; + end + p.connected = true; p:log("debug", "Successfully connected"); conn:setlistener(p.listeners, p.data); return p.listeners.onconnect(conn); @@ -73,6 +76,7 @@ log("warn", "Failed connection, but unexpected!"); return; end + p.conns[conn] = nil; p.last_error = reason or "unknown reason"; p:log("debug", "Connection attempt failed: %s", p.last_error); attempt_connection(p); @@ -85,6 +89,7 @@ listeners = assert(listeners); options = options or {}; data = data; + conns = {}; }, pending_connection_mt); p:log("debug", "Starting connection process");