From 4cc312d2d46d77bdabad0fa5753300f39787f901 Mon Sep 17 00:00:00 2001 From: Leo Correa Date: Tue, 12 Jan 2016 21:32:40 -0500 Subject: [PATCH] Add debug API call timing. Also, increased the timeout to 20 seconds which gives slow clients a bit more time to complete the calls --- cmd/debug.go | 73 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 61 insertions(+), 12 deletions(-) diff --git a/cmd/debug.go b/cmd/debug.go index 26cf0edbd..9fc89487b 100644 --- a/cmd/debug.go +++ b/cmd/debug.go @@ -6,6 +6,7 @@ import ( "net/http" "os" "runtime" + "sync" "time" "github.com/codegangsta/cli" @@ -13,16 +14,23 @@ import ( "github.com/exercism/cli/paths" ) +type pingResult struct { + URL string + Service string + Status string + Latency time.Duration +} + // Debug provides information about the user's environment and configuration. func Debug(ctx *cli.Context) { defer fmt.Printf("\nIf you are having trouble and need to file a GitHub issue (https://github.com/exercism/exercism.io/issues) please include this information (except your API key. Keep that private).\n") - client := http.Client{Timeout: 5 * time.Second} + client := &http.Client{Timeout: 20 * time.Second} fmt.Printf("\n**** Debug Information ****\n") fmt.Printf("Exercism CLI Version: %s\n", ctx.App.Version) - rel, err := fetchLatestRelease(client) + rel, err := fetchLatestRelease(*client) if err != nil { log.Println("unable to fetch latest release: " + err.Error()) } else { @@ -61,18 +69,59 @@ func Debug(ctx *cli.Context) { fmt.Println("Config file: ") fmt.Println("API Key: ") } - - fmt.Printf("API: %s [%s]\n", c.API, pingURL(client, c.API)) - fmt.Printf("XAPI: %s [%s]\n", c.XAPI, pingURL(client, c.XAPI)) fmt.Printf("Exercises Directory: %s\n", c.Dir) -} -func pingURL(client http.Client, url string) string { - res, err := client.Get(url) - if err != nil { - return err.Error() + fmt.Println("Testing API endpoints reachability") + + endpoints := map[string]string{ + "API": c.API, + "XAPI": c.XAPI, + "GitHub API": "https://api.github.com/", } - defer res.Body.Close() - return "connected" + var wg sync.WaitGroup + results := make(chan pingResult) + defer close(results) + + wg.Add(len(endpoints)) + + for service, url := range endpoints { + go func(service, url string) { + now := time.Now() + res, err := client.Get(url) + delta := time.Since(now) + if err != nil { + results <- pingResult{ + URL: url, + Service: service, + Status: err.Error(), + Latency: delta, + } + return + } + defer res.Body.Close() + + results <- pingResult{ + URL: url, + Service: service, + Status: "connected", + Latency: delta, + } + }(service, url) + } + + go func() { + for r := range results { + fmt.Printf( + "\t* %s: %s [%s] %s\n", + r.Service, + r.URL, + r.Status, + r.Latency, + ) + wg.Done() + } + }() + + wg.Wait() }