Move fire and forget code from metrics.Track() (used only by CLI) to metrics.Send (used by both CLI and API)

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-09-15 17:28:23 +02:00
parent 2570ebec86
commit e56061d27c
2 changed files with 28 additions and 27 deletions

View File

@ -64,10 +64,24 @@ func NewClient() Client {
} }
func (c *client) Send(command Command) { func (c *client) Send(command Command) {
req, err := json.Marshal(command) wasIn := make(chan bool)
if err != nil {
return // Fire and forget, we don't want to slow down the user waiting for DD
} // metrics endpoint to respond. We could lose some events but that's ok.
go func() {
defer func() {
_ = recover()
}()
wasIn <- true
req, err := json.Marshal(command)
if err != nil {
return
}
_, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
}()
<-wasIn
_, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
} }

View File

@ -78,28 +78,15 @@ const (
// Track sends the tracking analytics to Docker Desktop // Track sends the tracking analytics to Docker Desktop
func Track(context string, args []string, flags *flag.FlagSet) { func Track(context string, args []string, flags *flag.FlagSet) {
wasIn := make(chan bool) command := getCommand(args, flags)
if command != "" {
// Fire and forget, we don't want to slow down the user waiting for DD c := NewClient()
// metrics endpoint to respond. We could lose some events but that's ok. c.Send(Command{
go func() { Command: command,
defer func() { Context: context,
_ = recover() Source: CLISource,
}() })
}
wasIn <- true
command := getCommand(args, flags)
if command != "" {
c := NewClient()
c.Send(Command{
Command: command,
Context: context,
Source: CLISource,
})
}
}()
<-wasIn
} }
func getCommand(args []string, flags *flag.FlagSet) string { func getCommand(args []string, flags *flag.FlagSet) string {