-
Notifications
You must be signed in to change notification settings - Fork 69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add net.LookipIP DNS provider implementation #208
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ package memberlist | |
|
||
import ( | ||
"context" | ||
"net" | ||
"sync" | ||
) | ||
|
||
// DNSProvider supports storing or resolving a list of addresses. | ||
|
@@ -13,3 +15,31 @@ type DNSProvider interface { | |
// Addresses returns the latest addresses present in the DNSProvider. | ||
Addresses() []string | ||
} | ||
|
||
type dnsProvider struct { | ||
sync.Mutex | ||
addr []string | ||
} | ||
|
||
func NewDNSProvider() DNSProvider { return &dnsProvider{} } | ||
|
||
func (d *dnsProvider) Resolve(ctx context.Context, addrs []string) error { | ||
d.Lock() | ||
defer d.Unlock() | ||
for _, a := range addrs { | ||
ips, err := net.LookupIP(a) | ||
if err != nil { | ||
return err | ||
} | ||
for _, ip := range ips { | ||
d.addr = append(d.addr, ip.String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of doing an var addrs []string
for _, a: range addrs {
// do the lookup
addrs = append(addrs, ip.String())
}
d.Lock()
d.addrs = addrs
d.Unlock()
return nil |
||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (d *dnsProvider) Addresses() []string { | ||
d.Lock() | ||
defer d.Unlock() | ||
return d.addr | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you need to make a copy of this otherwise the underlying storage of the slice can still be modified after the caller starts using it (I could be wrong, we do run tests with |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package memberlist | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
) | ||
|
||
func TestDNSProvider(t *testing.T) { | ||
dns := &dnsProvider{} | ||
if err := dns.Resolve(context.Background(), []string{"localhost"}); err != nil { | ||
t.Fatal(err) | ||
} | ||
has127_0_0_1 := false | ||
for _, addr := range dns.Addresses() { | ||
if addr == "127.0.0.1" { | ||
has127_0_0_1 = true | ||
} | ||
} | ||
if !has127_0_0_1 { | ||
t.Error("resolving localhost must result in 127.0.0.1 address", dns.Addresses()) | ||
} | ||
if err := dns.Resolve(context.Background(), []string{"invalid dns"}); err == nil { | ||
t.Error("resolving and invalid address must result in an error") | ||
} | ||
if len(dns.Addresses()) == 0 { | ||
t.Error("DNSProvider must keep recent addresses on failure") | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the context be checked for cancellation on each iteration? That seems like it'd make this more responsive.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or perhaps just once before doing any DNS lookups?