Skip to content

Commit

Permalink
Add TTL server option
Browse files Browse the repository at this point in the history
This deprecates the Server.TTL method which has a data race
(detected with Go's race detector).

Fixes libp2p#18
  • Loading branch information
betamos committed Apr 20, 2022
1 parent 92498da commit 7b993ea
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,36 @@ const (

var defaultTTL uint32 = 3200

type serverOpts struct {
ttl uint32
}

func applyServerOpts(options ...ServerOption) serverOpts {
// Apply default configuration and load supplied options.
var conf = serverOpts{
ttl: defaultTTL,
}
for _, o := range options {
if o != nil {
o(&conf)
}
}
return conf
}

// ServerOption fills the option struct.
type ServerOption func(*serverOpts)

// TTL sets the TTL for DNS replies.
func TTL(ttl uint32) ServerOption {
return func(o *serverOpts) {
o.ttl = ttl
}
}

// Register a service by given arguments. This call will take the system's hostname
// and lookup IP by that hostname.
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface) (*Server, error) {
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
entry := newServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text
Expand Down Expand Up @@ -68,7 +95,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces
return nil, fmt.Errorf("could not determine host IP addresses")
}

s, err := newServer(ifaces)
s, err := newServer(ifaces, applyServerOpts(opts...))
if err != nil {
return nil, err
}
Expand All @@ -81,7 +108,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces

// RegisterProxy registers a service proxy. This call will skip the hostname/IP lookup and
// will use the provided values.
func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface) (*Server, error) {
func RegisterProxy(instance, service, domain string, port int, host string, ips []string, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
entry := newServiceEntry(instance, service, domain)
entry.Port = port
entry.Text = text
Expand Down Expand Up @@ -124,7 +151,7 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
ifaces = listMulticastInterfaces()
}

s, err := newServer(ifaces)
s, err := newServer(ifaces, applyServerOpts(opts...))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,7 +181,7 @@ type Server struct {
}

// Constructs server structure
func newServer(ifaces []net.Interface) (*Server, error) {
func newServer(ifaces []net.Interface, opts serverOpts) (*Server, error) {
ipv4conn, err4 := joinUdp4Multicast(ifaces)
if err4 != nil {
log.Printf("[zeroconf] no suitable IPv4 interface: %s", err4.Error())
Expand All @@ -172,7 +199,7 @@ func newServer(ifaces []net.Interface) (*Server, error) {
ipv4conn: ipv4conn,
ipv6conn: ipv6conn,
ifaces: ifaces,
ttl: defaultTTL,
ttl: opts.ttl,
shouldShutdown: make(chan struct{}),
}

Expand All @@ -199,6 +226,8 @@ func (s *Server) SetText(text []string) {
}

// TTL sets the TTL for DNS replies
//
// Deprecated: This method is racy. Use the TTL server option instead.
func (s *Server) TTL(ttl uint32) {
s.ttl = ttl
}
Expand Down

0 comments on commit 7b993ea

Please sign in to comment.