net.connect: Add API to create custom connect()s with options, incl. use_ipv[46]
authorMatthew Wild <mwild1@gmail.com>
Fri, 24 Jan 2020 13:48:49 +0000
changeset 10616 44ef46e1a951
parent 10615 c10511380c0f
child 10617 74d66b1be989
net.connect: Add API to create custom connect()s with options, incl. use_ipv[46]
net/connect.lua
--- a/net/connect.lua	Thu Jan 23 21:59:43 2020 +0000
+++ b/net/connect.lua	Fri Jan 24 13:48:49 2020 +0000
@@ -2,13 +2,17 @@
 local log = require "util.logger".init("net.connect");
 local new_id = require "util.id".short;
 
--- TODO Respect use_ipv4, use_ipv6
 -- TODO #1246 Happy Eyeballs
 -- FIXME RFC 6724
 -- FIXME Error propagation from resolvers doesn't work
 -- FIXME #1428 Reuse DNS resolver object between service and basic resolver
 -- FIXME #1429 Close DNS resolver object when done
 
+local default_connector_options = {
+	use_ipv4 = true;
+	use_ipv6 = true;
+};
+
 local pending_connection_methods = {};
 local pending_connection_mt = {
 	__name = "pending_connection";
@@ -78,19 +82,24 @@
 	attempt_connection(p);
 end
 
-local function connect(target_resolver, listeners, options, data)
-	local p = setmetatable({
-		id = new_id();
-		target_resolver = target_resolver;
-		listeners = assert(listeners);
-		options = options or {};
-		data = data;
-	}, pending_connection_mt);
+local function new_connector(connector_options)
+	local function connect(target_resolver, listeners, options, data)
+		local p = setmetatable({
+			id = new_id();
+			target_resolver = target_resolver;
+			listeners = assert(listeners);
+			options = options or {};
+			data = data;
+			connector_options = connector_options or default_connector_options;
+		}, pending_connection_mt);
 
-	p:log("debug", "Starting connection process");
-	attempt_connection(p);
+		p:log("debug", "Starting connection process");
+		attempt_connection(p);
+	end
+	return connect;
 end
 
 return {
-	connect = connect;
+	connect = new_connector(default_connector_options);
+	new_connector = new_connector;
 };