From 5b2a13da742bfaedd52e957c488743a91b364604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Didrik=20Nordstr=C3=B6m?= Date: Thu, 21 Apr 2022 02:35:38 -0700 Subject: [PATCH] Add TTL server option (#23) This deprecates the Server.TTL method which has a data race (detected with Go's race detector). Fixes #4 --- server.go | 41 +++++++++++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 6 deletions(-) diff --git a/server.go b/server.go index ab1b3486..fb4b4976 100644 --- a/server.go +++ b/server.go @@ -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 @@ -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 } @@ -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 @@ -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 } @@ -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()) @@ -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{}), } @@ -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 }