Comparison

net/connect.lua @ 10612:44ef46e1a951

net.connect: Add API to create custom connect()s with options, incl. use_ipv[46]
author Matthew Wild <mwild1@gmail.com>
date Fri, 24 Jan 2020 13:48:49 +0000
parent 10485:913276ba0c47
child 10623:f51c88baeb8a
comparison
equal deleted inserted replaced
10611:c10511380c0f 10612:44ef46e1a951
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 4
5 -- TODO Respect use_ipv4, use_ipv6
6 -- TODO #1246 Happy Eyeballs 5 -- TODO #1246 Happy Eyeballs
7 -- FIXME RFC 6724 6 -- FIXME RFC 6724
8 -- FIXME Error propagation from resolvers doesn't work 7 -- FIXME Error propagation from resolvers doesn't work
9 -- FIXME #1428 Reuse DNS resolver object between service and basic resolver 8 -- FIXME #1428 Reuse DNS resolver object between service and basic resolver
10 -- FIXME #1429 Close DNS resolver object when done 9 -- FIXME #1429 Close DNS resolver object when done
10
11 local default_connector_options = {
12 use_ipv4 = true;
13 use_ipv6 = true;
14 };
11 15
12 local pending_connection_methods = {}; 16 local pending_connection_methods = {};
13 local pending_connection_mt = { 17 local pending_connection_mt = {
14 __name = "pending_connection"; 18 __name = "pending_connection";
15 __index = pending_connection_methods; 19 __index = pending_connection_methods;
76 p.last_error = reason or "unknown reason"; 80 p.last_error = reason or "unknown reason";
77 p:log("debug", "Connection attempt failed: %s", p.last_error); 81 p:log("debug", "Connection attempt failed: %s", p.last_error);
78 attempt_connection(p); 82 attempt_connection(p);
79 end 83 end
80 84
81 local function connect(target_resolver, listeners, options, data) 85 local function new_connector(connector_options)
82 local p = setmetatable({ 86 local function connect(target_resolver, listeners, options, data)
83 id = new_id(); 87 local p = setmetatable({
84 target_resolver = target_resolver; 88 id = new_id();
85 listeners = assert(listeners); 89 target_resolver = target_resolver;
86 options = options or {}; 90 listeners = assert(listeners);
87 data = data; 91 options = options or {};
88 }, pending_connection_mt); 92 data = data;
93 connector_options = connector_options or default_connector_options;
94 }, pending_connection_mt);
89 95
90 p:log("debug", "Starting connection process"); 96 p:log("debug", "Starting connection process");
91 attempt_connection(p); 97 attempt_connection(p);
98 end
99 return connect;
92 end 100 end
93 101
94 return { 102 return {
95 connect = connect; 103 connect = new_connector(default_connector_options);
104 new_connector = new_connector;
96 }; 105 };