-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresolver.go
66 lines (60 loc) · 1.27 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package netlib
import (
"net"
"sync"
"github.com/gigawattio/errorlib"
)
type DnsResolution struct {
DomainName string
Ips []string
Error error
}
type DnsResolutions struct {
Results []DnsResolution
}
// BulkResolver4 resolves a list of domain names into their ipv4 addresses.
func BulkResolver4(names ...string) DnsResolutions {
var (
r = DnsResolutions{
Results: make([]DnsResolution, len(names)),
}
wg sync.WaitGroup
)
for i, name := range names {
if name != "" {
wg.Add(1)
go func(i int, name string) {
if ip4address, err := net.ResolveIPAddr("ip4", name); err != nil {
r.Results[i] = DnsResolution{
DomainName: name,
Ips: nil,
Error: err,
}
} else {
r.Results[i] = DnsResolution{
DomainName: name,
Ips: []string{ip4address.String()},
Error: nil,
}
}
wg.Done()
}(i, name)
}
}
wg.Wait()
return r
}
// Return a single combined squashed together error if there were any errors in
// any of the resolution tasks.
func (rs *DnsResolutions) AnyErrors() (err error) {
errors := []error{}
for _, r := range rs.Results {
if r.Error != nil {
errors = append(errors, r.Error)
}
}
if err = errorlib.Merge(errors); err != nil {
return
}
return
}