@@ -177,6 +177,8 @@ func newClient(opts clientOpts) (*client, error) {
177177 }, nil
178178}
179179
180+ var cleanupFreq = 10 * time .Second
181+
180182// Start listeners and waits for the shutdown signal from exit channel
181183func (c * client ) mainloop (ctx context.Context , params * lookupParams ) {
182184 // start listening for responses
@@ -189,16 +191,28 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
189191 }
190192
191193 // Iterate through channels from listeners goroutines
192- var entries , sentEntries map [string ]* ServiceEntry
193- sentEntries = make (map [string ]* ServiceEntry )
194+ var entries map [string ]* ServiceEntry
195+ sentEntries := make (map [string ]* ServiceEntry )
196+
197+ ticker := time .NewTicker (cleanupFreq )
198+ defer ticker .Stop ()
194199 for {
200+ var now time.Time
195201 select {
196202 case <- ctx .Done ():
197203 // Context expired. Notify subscriber that we are done here.
198204 params .done ()
199205 c .shutdown ()
200206 return
207+ case t := <- ticker .C :
208+ for k , e := range sentEntries {
209+ if t .After (e .Expiry ) {
210+ delete (sentEntries , k )
211+ }
212+ }
213+ continue
201214 case msg := <- msgCh :
215+ now = time .Now ()
202216 entries = make (map [string ]* ServiceEntry )
203217 sections := append (msg .Answer , msg .Ns ... )
204218 sections = append (sections , msg .Extra ... )
@@ -218,7 +232,7 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
218232 params .Service ,
219233 params .Domain )
220234 }
221- entries [rr .Ptr ].TTL = rr .Hdr .Ttl
235+ entries [rr .Ptr ].Expiry = now . Add ( time . Duration ( rr .Hdr .Ttl ) * time . Second )
222236 case * dns.SRV :
223237 if params .ServiceInstanceName () != "" && params .ServiceInstanceName () != rr .Hdr .Name {
224238 continue
@@ -233,7 +247,7 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
233247 }
234248 entries [rr .Hdr .Name ].HostName = rr .Target
235249 entries [rr .Hdr .Name ].Port = int (rr .Port )
236- entries [rr .Hdr .Name ].TTL = rr .Hdr .Ttl
250+ entries [rr .Hdr .Name ].Expiry = now . Add ( time . Duration ( rr .Hdr .Ttl ) * time . Second )
237251 case * dns.TXT :
238252 if params .ServiceInstanceName () != "" && params .ServiceInstanceName () != rr .Hdr .Name {
239253 continue
@@ -247,7 +261,7 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
247261 params .Domain )
248262 }
249263 entries [rr .Hdr .Name ].Text = rr .Txt
250- entries [rr .Hdr .Name ].TTL = rr .Hdr .Ttl
264+ entries [rr .Hdr .Name ].Expiry = now . Add ( time . Duration ( rr .Hdr .Ttl ) * time . Second )
251265 }
252266 }
253267 // Associate IPs in a second round as other fields should be filled by now.
@@ -271,7 +285,7 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
271285
272286 if len (entries ) > 0 {
273287 for k , e := range entries {
274- if e . TTL == 0 {
288+ if ! e . Expiry . After ( now ) {
275289 delete (entries , k )
276290 delete (sentEntries , k )
277291 continue
0 commit comments