Software / code / prosody
Comparison
net/connect.lua @ 12412:18a3a6218100
net.connect: When more targets are immediately available, try them after a delay
RFC 8305
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 18 Mar 2022 16:16:57 +0000 |
| parent | 12411:e132a4279914 |
| child | 12425:eabcc3ae22e9 |
comparison
equal
deleted
inserted
replaced
| 12411:e132a4279914 | 12412:18a3a6218100 |
|---|---|
| 1 local server = require "net.server"; | 1 local server = require "net.server"; |
| 2 local log = require "util.logger".init("net.connect"); | 2 local log = require "util.logger".init("net.connect"); |
| 3 local new_id = require "util.id".short; | 3 local new_id = require "util.id".short; |
| 4 local timer = require "util.timer"; | |
| 4 | 5 |
| 5 -- TODO #1246 Happy Eyeballs | 6 -- TODO #1246 Happy Eyeballs |
| 6 -- FIXME RFC 6724 | 7 -- FIXME RFC 6724 |
| 7 -- FIXME Error propagation from resolvers doesn't work | 8 -- FIXME Error propagation from resolvers doesn't work |
| 8 -- FIXME #1428 Reuse DNS resolver object between service and basic resolver | 9 -- FIXME #1428 Reuse DNS resolver object between service and basic resolver |
| 26 | 27 |
| 27 local pending_connection_listeners = {}; | 28 local pending_connection_listeners = {}; |
| 28 | 29 |
| 29 local function attempt_connection(p) | 30 local function attempt_connection(p) |
| 30 p:log("debug", "Checking for targets..."); | 31 p:log("debug", "Checking for targets..."); |
| 31 p.target_resolver:next(function (conn_type, ip, port, extra) | 32 p.target_resolver:next(function (conn_type, ip, port, extra, more_targets_available) |
| 32 if not conn_type then | 33 if not conn_type then |
| 33 -- No more targets to try | 34 -- No more targets to try |
| 34 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); |
| 35 if p.listeners.onfail then | 36 if p.listeners.onfail then |
| 36 p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service"); | 37 p.listeners.onfail(p.data, p.last_error or p.target_resolver.last_error or "unable to resolve service"); |
| 45 p.last_error = err or "unknown reason"; | 46 p.last_error = err or "unknown reason"; |
| 46 return attempt_connection(p); | 47 return attempt_connection(p); |
| 47 end | 48 end |
| 48 p.conns[conn] = true; | 49 p.conns[conn] = true; |
| 49 pending_connections_map[conn] = p; | 50 pending_connections_map[conn] = p; |
| 51 if more_targets_available then | |
| 52 timer.add_task(0.250, function () | |
| 53 if not p.connected then | |
| 54 p:log("debug", "Still not connected, making parallel connection attempt..."); | |
| 55 attempt_connection(p); | |
| 56 end | |
| 57 end); | |
| 58 end | |
| 50 end); | 59 end); |
| 51 end | 60 end |
| 52 | 61 |
| 53 function pending_connection_listeners.onconnect(conn) | 62 function pending_connection_listeners.onconnect(conn) |
| 54 local p = pending_connections_map[conn]; | 63 local p = pending_connections_map[conn]; |