Skip to content

Commit

Permalink
mdns: simplify client close
Browse files Browse the repository at this point in the history
  • Loading branch information
ignoramous committed Sep 18, 2023
1 parent 2ab6be2 commit fc112bb
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions intra/dns53/mdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ type client struct {

oneshot bool

closed atomic.Int32
closedCh chan struct{}
closed atomic.Int32
}

// newClient creates a new mdns unicast and multicast client
Expand Down Expand Up @@ -290,7 +289,6 @@ func (t *dnssd) newClient(oneshot bool) (*client, error) {
unicast6: uconn6,
tracker: make(map[string]*dnssdanswer),
msgCh: make(chan *dns.Msg, 32),
closedCh: make(chan struct{}),
oneshot: oneshot,
}
return c, nil
Expand All @@ -304,10 +302,6 @@ func (c *client) Close() error {

log.I("mdns: closing client %v", *c)

c.closedCh <- struct{}{}
defer close(c.closedCh)
defer close(c.msgCh)

closeudp(c.unicast4)
closeudp(c.unicast6)
closeudp(c.multicast4)
Expand Down Expand Up @@ -501,15 +495,16 @@ func (c *client) recv(conn *net.UDPConn) {
log.E("mdns: recv: unpack failed: %v", err)
continue
}

timesup := time.After(timeout)
// ideally, the writer would close the channel, but in this
// case there are potentially 4 writers (2 unicast, 2 multicast)
// so we rely on client.Close and the closedCh to signal
// the wrtier to stop; also see: go.dev/play/p/gzwnGAFlTDV
// also see: go.dev/play/p/gzwnGAFlTDV
select {
case c.msgCh <- msg:
log.V("mdns: recv: from(%v); sent; bytes(%d)", raddr, n)
case _, ok := <-c.closedCh:
log.V("mdns: recv: from(%v); closed; ch(%t); bytes(%d)", raddr, ok, n)
case <-timesup:
log.V("mdns: recv: from(%v); timeout ch; bytes(%d)", raddr, n)
return
}
}
Expand Down

1 comment on commit fc112bb

@ignoramous
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.