lib: Replace done channel with contexts in and add names to util services (#6166)

This commit is contained in:
Simon Frei
2019-11-21 08:41:15 +01:00
committed by GitHub
parent 552ea68672
commit 90d85fd0a2
34 changed files with 240 additions and 218 deletions

View File

@@ -8,6 +8,7 @@ package discover
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
@@ -128,7 +129,7 @@ func NewGlobal(server string, cert tls.Certificate, addrList AddressLister, evLo
noLookup: opts.noLookup,
evLogger: evLogger,
}
cl.Service = util.AsService(cl.serve)
cl.Service = util.AsService(cl.serve, cl.String())
if !opts.noAnnounce {
// If we are supposed to annonce, it's an error until we've done so.
cl.setError(errors.New("not announced"))
@@ -188,11 +189,11 @@ func (c *globalClient) String() string {
return "global@" + c.server
}
func (c *globalClient) serve(stop chan struct{}) {
func (c *globalClient) serve(ctx context.Context) {
if c.noAnnounce {
// We're configured to not do announcements, only lookups. To maintain
// the same interface, we just pause here if Serve() is run.
<-stop
<-ctx.Done()
return
}
@@ -212,7 +213,7 @@ func (c *globalClient) serve(stop chan struct{}) {
case <-timer.C:
c.sendAnnouncement(timer)
case <-stop:
case <-ctx.Done():
return
}
}