zeroconf icon indicating copy to clipboard operation
zeroconf copied to clipboard

Resolver API design

Open marten-seemann opened this issue 4 years ago • 2 comments

I'm working on a clean shutdown procedure for the client / resolver, and I'm unsure how to proceed because I'm a bit confused by the API.

To start a mDNS client, you first create a new resolver, and then call Lookup or Browse:

r, _ := NewResolver()
entries := make(chan *ServiceEntry)
err := r.Browse(ctx, "service", "domain", entries)

This raises 2 problems:

  1. Both Browse and Lookup return immediately. The go routines started by these methods (including the Go routine that reads from the connection) is stopped eventually after the context is done, but there's no way to deterministically tell when this happened.
  2. Although the API seems to suggest otherwise, it's only possible to call Lookup / Browse once per Resolver instance, as both of them start reading from the UDPConn (and it only makes sense to read from a connection with a single Go routine).

I can see 2 fundamentally different ways to resolve this:

  1. Change the API in a way that doesn't suggest that Browse and Lookup can be called multiple times.
    1. This would probably mean to remove the Resolver, and expose Browse and Lookup as top-level functions.
    2. Make Browse and Lookup block until all Go routines have stopped.
  2. Make it possible to call Browse and Lookup concurrently. This would require us to have some demultiplexing logic, to associate received message with a particular lookup. This is easy for responses to a query (they come with an ID), but I'm not sure how one would implement this for the unrequested announcements that servers send out regularly.

Any thoughts, @grandcat and @Stebalien?

marten-seemann avatar Jul 04 '21 19:07 marten-seemann

Both Browse and Lookup return immediately. The go routines started by these methods (including the Go routine that reads from the connection) is stopped eventually after the context is done, but there's no way to deterministically tell when this happened.

The entries channel is closed, right?

Stebalien avatar Jul 08 '21 04:07 Stebalien

Although the API seems to suggest otherwise, it's only possible to call Lookup / Browse once per Resolver instance, as both of them start reading from the UDPConn (and it only makes sense to read from a connection with a single Go routine).

More importantly, it looks like they're clearly "single use only" (i.e., it calls "shutdown" after one use).

So I'd either:

  1. Do as you say and have separate free functions.
  2. Store the clientOpts inside the Resolver instead of a full client, constructing new clients on demand.

Stebalien avatar Jul 08 '21 04:07 Stebalien