Software /
code /
prosody
Annotate
net/resolvers/chain.lua @ 13014:06453c564141
util.startup: Add prosody.started promise to easily execute code after startup
To avoid a race where server-started fires before the promise function body is
run (on next tick), I moved server-started to fire on the next tick, which
seems sensible anyway.
Errors are logged, I'm not sure if we ought to be doing something more here.
I'm sure we'll find out.
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sat, 01 Apr 2023 11:56:38 +0100 |
parent | 12204:7c397a49d163 |
rev | line source |
---|---|
12204
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 local methods = {}; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local resolver_mt = { __index = methods }; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- Find the next target to connect to, and |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- pass it to cb() |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 function methods:next(cb) |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 if self.resolvers then |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 if not self.resolver then |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 if #self.resolvers == 0 then |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 cb(nil); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 return; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local next_resolver = table.remove(self.resolvers, 1); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 self.resolver = next_resolver; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 self.resolver:next(function (...) |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 if self.resolver then |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 self.last_error = self.resolver.last_error; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 if ... == nil then |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 self.resolver = nil; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 self:next(cb); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 else |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 cb(...); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 end); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 return; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 local function new(resolvers) |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 return setmetatable({ resolvers = resolvers }, resolver_mt); |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 end |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 return { |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 new = new; |
7c397a49d163
net.resolvers.chain: A resolver for combining other resolvers
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 }; |