Skip to content

Commit

Permalink
Merge pull request #92 from marten-seemann/fix-browsing
Browse files Browse the repository at this point in the history
don't stop querying when browsing
  • Loading branch information
grandcat authored Jun 10, 2021
2 parents f1c8d74 + 2c53f0f commit 55022d7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
11 changes: 5 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func (r *Resolver) Browse(ctx context.Context, service, domain string, entries c
params.Domain = domain
}
params.Entries = entries
params.isBrowsing = true
ctx, cancel := context.WithCancel(ctx)
go r.c.mainloop(ctx, params)

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

// defaultParams returns a default set of QueryParams.
func defaultParams(service string) *lookupParams {
return newLookupParams("", service, "local", make(chan *ServiceEntry))
return newLookupParams("", service, "local", false, make(chan *ServiceEntry))
}

// Client structure encapsulates both IPv4/IPv6 UDP connections.
Expand Down Expand Up @@ -293,7 +294,9 @@ func (c *client) mainloop(ctx context.Context, params *lookupParams) {
// service entry.
params.Entries <- e
sentEntries[k] = e
params.disableProbing()
if !params.isBrowsing {
params.disableProbing()
}
}
// reset entries
entries = make(map[string]*ServiceEntry)
Expand Down Expand Up @@ -368,10 +371,6 @@ func (c *client) recv(ctx context.Context, l interface{}, msgCh chan *dns.Msg) {
// TODO: move error reporting to shutdown function as periodicQuery is called from
// go routine context.
func (c *client) periodicQuery(ctx context.Context, params *lookupParams) error {
if params.stopProbing == nil {
return nil
}

bo := backoff.NewExponentialBackOff()
bo.InitialInterval = 4 * time.Second
bo.MaxInterval = 60 * time.Second
Expand Down
12 changes: 8 additions & 4 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,22 @@ type lookupParams struct {
ServiceRecord
Entries chan<- *ServiceEntry // Entries Channel

isBrowsing bool
stopProbing chan struct{}
once sync.Once
}

// newLookupParams constructs a lookupParams.
func newLookupParams(instance, service, domain string, entries chan<- *ServiceEntry) *lookupParams {
return &lookupParams{
func newLookupParams(instance, service, domain string, isBrowsing bool, entries chan<- *ServiceEntry) *lookupParams {
p := &lookupParams{
ServiceRecord: *NewServiceRecord(instance, service, domain),
Entries: entries,

stopProbing: make(chan struct{}),
isBrowsing: isBrowsing,
}
if !isBrowsing {
p.stopProbing = make(chan struct{})
}
return p
}

// Notify subscriber that no more entries will arrive. Mostly caused
Expand Down
14 changes: 10 additions & 4 deletions service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,16 @@ func TestSubtype(t *testing.T) {
}
entries := make(chan *ServiceEntry)
var expectedResult []*ServiceEntry
go func(results <-chan *ServiceEntry) {
s := <-results
expectedResult = append(expectedResult, s)
}(entries)
go func() {
for {
select {
case s := <-entries:
expectedResult = append(expectedResult, s)
case <-ctx.Done():
return
}
}
}()

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

0 comments on commit 55022d7

Please sign in to comment.