Skip to content

Commit 55022d7

Browse files
authored
Merge pull request #92 from marten-seemann/fix-browsing
don't stop querying when browsing
2 parents f1c8d74 + 2c53f0f commit 55022d7

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (r *Resolver) Browse(ctx context.Context, service, domain string, entries c
8686
params.Domain = domain
8787
}
8888
params.Entries = entries
89+
params.isBrowsing = true
8990
ctx, cancel := context.WithCancel(ctx)
9091
go r.c.mainloop(ctx, params)
9192

@@ -134,7 +135,7 @@ func (r *Resolver) Lookup(ctx context.Context, instance, service, domain string,
134135

135136
// defaultParams returns a default set of QueryParams.
136137
func defaultParams(service string) *lookupParams {
137-
return newLookupParams("", service, "local", make(chan *ServiceEntry))
138+
return newLookupParams("", service, "local", false, make(chan *ServiceEntry))
138139
}
139140

140141
// Client structure encapsulates both IPv4/IPv6 UDP connections.
@@ -293,7 +294,9 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
293294
// service entry.
294295
params.Entries <- e
295296
sentEntries[k] = e
296-
params.disableProbing()
297+
if !params.isBrowsing {
298+
params.disableProbing()
299+
}
297300
}
298301
// reset entries
299302
entries = make(map[string]*ServiceEntry)
@@ -368,10 +371,6 @@ func (c *client) recv(ctx context.Context, l interface{}, msgCh chan *dns.Msg) {
368371
// TODO: move error reporting to shutdown function as periodicQuery is called from
369372
// go routine context.
370373
func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error {
371-
if params.stopProbing == nil {
372-
return nil
373-
}
374-
375374
bo := backoff.NewExponentialBackOff()
376375
bo.InitialInterval = 4 * time.Second
377376
bo.MaxInterval = 60 * time.Second

service.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,22 @@ type lookupParams struct {
7070
ServiceRecord
7171
Entries chan<- *ServiceEntry // Entries Channel
7272

73+
isBrowsing bool
7374
stopProbing chan struct{}
7475
once sync.Once
7576
}
7677

7778
// newLookupParams constructs a lookupParams.
78-
func newLookupParams(instance, service, domain string, entries chan<- *ServiceEntry) *lookupParams {
79-
return &lookupParams{
79+
func newLookupParams(instance, service, domain string, isBrowsing bool, entries chan<- *ServiceEntry) *lookupParams {
80+
p := &lookupParams{
8081
ServiceRecord: *NewServiceRecord(instance, service, domain),
8182
Entries: entries,
82-
83-
stopProbing: make(chan struct{}),
83+
isBrowsing: isBrowsing,
84+
}
85+
if !isBrowsing {
86+
p.stopProbing = make(chan struct{})
8487
}
88+
return p
8589
}
8690

8791
// Notify subscriber that no more entries will arrive. Mostly caused

service_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,16 @@ func TestSubtype(t *testing.T) {
152152
}
153153
entries := make(chan *ServiceEntry)
154154
var expectedResult []*ServiceEntry
155-
go func(results <-chan *ServiceEntry) {
156-
s := <-results
157-
expectedResult = append(expectedResult, s)
158-
}(entries)
155+
go func() {
156+
for {
157+
select {
158+
case s := <-entries:
159+
expectedResult = append(expectedResult, s)
160+
case <-ctx.Done():
161+
return
162+
}
163+
}
164+
}()
159165

160166
if err := resolver.Browse(ctx, mdnsService, mdnsDomain, entries); err != nil {
161167
t.Fatalf("Expected browse success, but got %v", err)

0 commit comments

Comments
 (0)