Changeset

12204:7c397a49d163

net.resolvers.chain: A resolver for combining other resolvers Say if you wanted to try both _xmpp and _xmpps services
author Kim Alvefur <zash@zash.se>
date Fri, 21 Jan 2022 17:56:20 +0100
parents 12203:320de3e4b579
children 12205:a2e6605303fa
files net/resolvers/chain.lua
diffstat 1 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/resolvers/chain.lua	Fri Jan 21 17:56:20 2022 +0100
@@ -0,0 +1,38 @@
+
+local methods = {};
+local resolver_mt = { __index = methods };
+
+-- Find the next target to connect to, and
+-- pass it to cb()
+function methods:next(cb)
+	if self.resolvers then
+		if not self.resolver then
+			if #self.resolvers == 0 then
+				cb(nil);
+				return;
+			end
+			local next_resolver = table.remove(self.resolvers, 1);
+			self.resolver = next_resolver;
+		end
+		self.resolver:next(function (...)
+			if self.resolver then
+				self.last_error = self.resolver.last_error;
+			end
+			if ... == nil then
+				self.resolver = nil;
+				self:next(cb);
+			else
+				cb(...);
+			end
+		end);
+		return;
+	end
+end
+
+local function new(resolvers)
+	return setmetatable({ resolvers = resolvers }, resolver_mt);
+end
+
+return {
+	new = new;
+};