Przeglądaj źródła

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 <[email protected]>
Guillaume Tardif 5 lat temu
rodzic
commit
e56061d27c
2 zmienionych plików z 28 dodań i 27 usunięć
  1. 19 5
      metrics/client.go
  2. 9 22
      metrics/metrics.go

+ 19 - 5
metrics/client.go

@@ -64,10 +64,24 @@ func NewClient() Client {
 }
 
 func (c *client) Send(command Command) {
-	req, err := json.Marshal(command)
-	if err != nil {
-		return
-	}
+	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.
+	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))
 }

+ 9 - 22
metrics/metrics.go

@@ -78,28 +78,15 @@ const (
 
 // Track sends the tracking analytics to Docker Desktop
 func Track(context string, args []string, flags *flag.FlagSet) {
-	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.
-	go func() {
-		defer func() {
-			_ = recover()
-		}()
-
-		wasIn <- true
-
-		command := getCommand(args, flags)
-		if command != "" {
-			c := NewClient()
-			c.Send(Command{
-				Command: command,
-				Context: context,
-				Source:  CLISource,
-			})
-		}
-	}()
-	<-wasIn
+	command := getCommand(args, flags)
+	if command != "" {
+		c := NewClient()
+		c.Send(Command{
+			Command: command,
+			Context: context,
+			Source:  CLISource,
+		})
+	}
 }
 
 func getCommand(args []string, flags *flag.FlagSet) string {