Skip to content

Commit 5b2a13d

Browse files
authored
Add TTL server option (#23)
This deprecates the Server.TTL method which has a data race (detected with Go's race detector). Fixes #4
1 parent 92498da commit 5b2a13d

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

server.go

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,36 @@ const (
2222

2323
var defaultTTL uint32 = 3200
2424

25+
type serverOpts struct {
26+
ttl uint32
27+
}
28+
29+
func applyServerOpts(options ...ServerOption) serverOpts {
30+
// Apply default configuration and load supplied options.
31+
var conf = serverOpts{
32+
ttl: defaultTTL,
33+
}
34+
for _, o := range options {
35+
if o != nil {
36+
o(&conf)
37+
}
38+
}
39+
return conf
40+
}
41+
42+
// ServerOption fills the option struct.
43+
type ServerOption func(*serverOpts)
44+
45+
// TTL sets the TTL for DNS replies.
46+
func TTL(ttl uint32) ServerOption {
47+
return func(o *serverOpts) {
48+
o.ttl = ttl
49+
}
50+
}
51+
2552
// Register a service by given arguments. This call will take the system's hostname
2653
// and lookup IP by that hostname.
27-
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface) (*Server, error) {
54+
func Register(instance, service, domain string, port int, text []string, ifaces []net.Interface, opts ...ServerOption) (*Server, error) {
2855
entry := newServiceEntry(instance, service, domain)
2956
entry.Port = port
3057
entry.Text = text
@@ -68,7 +95,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces
6895
return nil, fmt.Errorf("could not determine host IP addresses")
6996
}
7097

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

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

127-
s, err := newServer(ifaces)
154+
s, err := newServer(ifaces, applyServerOpts(opts...))
128155
if err != nil {
129156
return nil, err
130157
}
@@ -154,7 +181,7 @@ type Server struct {
154181
}
155182

156183
// Constructs server structure
157-
func newServer(ifaces []net.Interface) (*Server, error) {
184+
func newServer(ifaces []net.Interface, opts serverOpts) (*Server, error) {
158185
ipv4conn, err4 := joinUdp4Multicast(ifaces)
159186
if err4 != nil {
160187
log.Printf("[zeroconf] no suitable IPv4 interface: %s", err4.Error())
@@ -172,7 +199,7 @@ func newServer(ifaces []net.Interface) (*Server, error) {
172199
ipv4conn: ipv4conn,
173200
ipv6conn: ipv6conn,
174201
ifaces: ifaces,
175-
ttl: defaultTTL,
202+
ttl: opts.ttl,
176203
shouldShutdown: make(chan struct{}),
177204
}
178205

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

201228
// TTL sets the TTL for DNS replies
229+
//
230+
// Deprecated: This method is racy. Use the TTL server option instead.
202231
func (s *Server) TTL(ttl uint32) {
203232
s.ttl = ttl
204233
}

0 commit comments

Comments
 (0)