Skip to content

Commit

Permalink
feat: implement concurrent requests (#1)
Browse files Browse the repository at this point in the history
Implements concurrent requests via go channels.
  • Loading branch information
estahn committed Jan 13, 2019
1 parent a1aced9 commit 52a83cc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var output string
var rootCmd = &cobra.Command{
Use: "cloudping",
Short: "Returns the geographically closest region.",
Long: `cloudping identifies the cloud provider regions geographically closest
Long: `cloudping identifies the cloud provider regions geographically closest
and returns them in order of lowest to highest latency.`,
Run: func(cmd *cobra.Command, args []string) {
rs := endpoints.AwsPartition().Services()[dynamodb.EndpointsID].Regions()
Expand All @@ -53,15 +53,17 @@ and returns them in order of lowest to highest latency.`,
rs = makeEndpoints(regions)
}

ch := make(chan int)
pingResults := make(map[string]int, len(rs))

for region := range rs {
uri := fmt.Sprintf("https://dynamodb.%s.amazonaws.com/ping?x=%s", region, randStringBytes(12))
//println(uri)
pingResults[region] = pingURI(uri)
go func(ch chan<- int, uri string) { ch <- pingURI(uri) }(ch, uri)
}

//spew.Dump(pingResults)
for region := range rs {
pingResults[region] = <-ch
}

orderedResults := sortList(pingResults)

Expand Down

0 comments on commit 52a83cc

Please sign in to comment.