From 6f19bbfd5ec4f8ab50e85b2908475830201f71e9 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Fri, 18 Sep 2020 15:57:02 +0200 Subject: [PATCH] =?UTF-8?q?We=20can=E2=80=99t=20anymore=20=E2=80=9Cfire=20?= =?UTF-8?q?and=20forget=E2=80=9D,=20now=20that=20metrics=20get=20posted=20?= =?UTF-8?q?right=20at=20the=20end,=20most=20of=20the=20time=20we=E2=80=99d?= =?UTF-8?q?=20loose=20them.=20Give=20it=20max=2050=20ms=20to=20post=20metr?= =?UTF-8?q?ics,=20that=E2=80=99s=20plenty,=20post=20call=20ends=20in=20~2?= =?UTF-8?q?=20ms=20or=20less=20when=20desktop=20is=20up,=20less=20than=20o?= =?UTF-8?q?ne=20ms=20to=20fail=20the=20post=20when=20DD=20is=20not=20liste?= =?UTF-8?q?ning.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Guillaume Tardif --- metrics/client.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/metrics/client.go b/metrics/client.go index 93e440735..509599a16 100644 --- a/metrics/client.go +++ b/metrics/client.go @@ -22,6 +22,7 @@ import ( "encoding/json" "net" "net/http" + "time" ) type client struct { @@ -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 - - req, err := json.Marshal(command) - if err != nil { - return - } - - _, _ = c.httpClient.Post("http://localhost/usage", "application/json", bytes.NewBuffer(req)) + postMetrics(command, c) + result <- true }() - <-wasIn + + // 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)) + } }