Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
We can’t anymore “fire and forget”, now that metrics get posted right…
Browse files Browse the repository at this point in the history
… at the end, most of the time we’d loose them.

Give it max 50 ms to post metrics, that’s plenty, post call ends in ~2 ms or less when desktop is up, less than one ms to fail the post when DD is not listening.

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
  • Loading branch information
gtardif committed Sep 18, 2020
1 parent a71b2a3 commit 6f19bbf
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions metrics/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"encoding/json"
"net"
"net/http"
"time"
)

type client struct {
Expand Down Expand Up @@ -71,23 +72,23 @@ func NewClient() Client {
}

func (c *client) Send(command Command) {
wasIn := make(chan bool)

// 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.
result := make(chan bool, 1)
go func() {
defer func() {
_ = recover()
}()

wasIn <- true
postMetrics(command, c)
result <- true
}()

req, err := json.Marshal(command)
if err != nil {
return
}
// wait for the post finished, or timeout in case anything freezes.
// Posting metrics without Desktop listening returns in less than a ms, and a handful of ms (often <2ms) when Desktop is listening
select {
case <-result:
case <-time.After(50 * time.Millisecond):
}
}

func postMetrics(command Command, c *client) {
req, err := json.Marshal(command)
if err == nil {
_, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req))
}()
<-wasIn
}
}

0 comments on commit 6f19bbf

Please sign in to comment.