@@ -22,9 +22,36 @@ const (
22
22
23
23
var defaultTTL uint32 = 3200
24
24
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
+
25
52
// Register a service by given arguments. This call will take the system's hostname
26
53
// 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 ) {
28
55
entry := newServiceEntry (instance , service , domain )
29
56
entry .Port = port
30
57
entry .Text = text
@@ -68,7 +95,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces
68
95
return nil , fmt .Errorf ("could not determine host IP addresses" )
69
96
}
70
97
71
- s , err := newServer (ifaces )
98
+ s , err := newServer (ifaces , applyServerOpts ( opts ... ) )
72
99
if err != nil {
73
100
return nil , err
74
101
}
@@ -81,7 +108,7 @@ func Register(instance, service, domain string, port int, text []string, ifaces
81
108
82
109
// RegisterProxy registers a service proxy. This call will skip the hostname/IP lookup and
83
110
// 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 ) {
85
112
entry := newServiceEntry (instance , service , domain )
86
113
entry .Port = port
87
114
entry .Text = text
@@ -124,7 +151,7 @@ func RegisterProxy(instance, service, domain string, port int, host string, ips
124
151
ifaces = listMulticastInterfaces ()
125
152
}
126
153
127
- s , err := newServer (ifaces )
154
+ s , err := newServer (ifaces , applyServerOpts ( opts ... ) )
128
155
if err != nil {
129
156
return nil , err
130
157
}
@@ -154,7 +181,7 @@ type Server struct {
154
181
}
155
182
156
183
// Constructs server structure
157
- func newServer (ifaces []net.Interface ) (* Server , error ) {
184
+ func newServer (ifaces []net.Interface , opts serverOpts ) (* Server , error ) {
158
185
ipv4conn , err4 := joinUdp4Multicast (ifaces )
159
186
if err4 != nil {
160
187
log .Printf ("[zeroconf] no suitable IPv4 interface: %s" , err4 .Error ())
@@ -172,7 +199,7 @@ func newServer(ifaces []net.Interface) (*Server, error) {
172
199
ipv4conn : ipv4conn ,
173
200
ipv6conn : ipv6conn ,
174
201
ifaces : ifaces ,
175
- ttl : defaultTTL ,
202
+ ttl : opts . ttl ,
176
203
shouldShutdown : make (chan struct {}),
177
204
}
178
205
@@ -199,6 +226,8 @@ func (s *Server) SetText(text []string) {
199
226
}
200
227
201
228
// TTL sets the TTL for DNS replies
229
+ //
230
+ // Deprecated: This method is racy. Use the TTL server option instead.
202
231
func (s * Server ) TTL (ttl uint32 ) {
203
232
s .ttl = ttl
204
233
}
0 commit comments