Skip to content

Commit 51c4967

Browse files
committed
Merge pull request #271 from exercism/debug-timing
Add debug API call timing.
2 parents d8c09c9 + 4cc312d commit 51c4967

1 file changed

Lines changed: 61 additions & 12 deletions

File tree

cmd/debug.go

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,31 @@ import (
66
"net/http"
77
"os"
88
"runtime"
9+
"sync"
910
"time"
1011

1112
"github.com/codegangsta/cli"
1213
"github.com/exercism/cli/config"
1314
"github.com/exercism/cli/paths"
1415
)
1516

17+
type pingResult struct {
18+
URL string
19+
Service string
20+
Status string
21+
Latency time.Duration
22+
}
23+
1624
// Debug provides information about the user's environment and configuration.
1725
func Debug(ctx *cli.Context) {
1826
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")
1927

20-
client := http.Client{Timeout: 5 * time.Second}
28+
client := &http.Client{Timeout: 20 * time.Second}
2129

2230
fmt.Printf("\n**** Debug Information ****\n")
2331
fmt.Printf("Exercism CLI Version: %s\n", ctx.App.Version)
2432

25-
rel, err := fetchLatestRelease(client)
33+
rel, err := fetchLatestRelease(*client)
2634
if err != nil {
2735
log.Println("unable to fetch latest release: " + err.Error())
2836
} else {
@@ -61,18 +69,59 @@ func Debug(ctx *cli.Context) {
6169
fmt.Println("Config file: <not configured>")
6270
fmt.Println("API Key: <not configured>")
6371
}
64-
65-
fmt.Printf("API: %s [%s]\n", c.API, pingURL(client, c.API))
66-
fmt.Printf("XAPI: %s [%s]\n", c.XAPI, pingURL(client, c.XAPI))
6772
fmt.Printf("Exercises Directory: %s\n", c.Dir)
68-
}
6973

70-
func pingURL(client http.Client, url string) string {
71-
res, err := client.Get(url)
72-
if err != nil {
73-
return err.Error()
74+
fmt.Println("Testing API endpoints reachability")
75+
76+
endpoints := map[string]string{
77+
"API": c.API,
78+
"XAPI": c.XAPI,
79+
"GitHub API": "https://api.github.com/",
7480
}
75-
defer res.Body.Close()
7681

77-
return "connected"
82+
var wg sync.WaitGroup
83+
results := make(chan pingResult)
84+
defer close(results)
85+
86+
wg.Add(len(endpoints))
87+
88+
for service, url := range endpoints {
89+
go func(service, url string) {
90+
now := time.Now()
91+
res, err := client.Get(url)
92+
delta := time.Since(now)
93+
if err != nil {
94+
results <- pingResult{
95+
URL: url,
96+
Service: service,
97+
Status: err.Error(),
98+
Latency: delta,
99+
}
100+
return
101+
}
102+
defer res.Body.Close()
103+
104+
results <- pingResult{
105+
URL: url,
106+
Service: service,
107+
Status: "connected",
108+
Latency: delta,
109+
}
110+
}(service, url)
111+
}
112+
113+
go func() {
114+
for r := range results {
115+
fmt.Printf(
116+
"\t* %s: %s [%s] %s\n",
117+
r.Service,
118+
r.URL,
119+
r.Status,
120+
r.Latency,
121+
)
122+
wg.Done()
123+
}
124+
}()
125+
126+
wg.Wait()
78127
}

0 commit comments

Comments
 (0)