Skip to content

Instantly share code, notes, and snippets.

@gerep
Created February 23, 2016 16:43
Show Gist options
  • Save gerep/52a64cddf7d572d273f5 to your computer and use it in GitHub Desktop.
Save gerep/52a64cddf7d572d273f5 to your computer and use it in GitHub Desktop.
Using channels to test the fastest CDN response
package main
import (
"fmt"
"net/http"
)
type response struct {
name string
resp *http.Response
url string
}
func get(name string, url string, r chan response) {
if resp, err := http.Get(url); err == nil {
r <- response{name, resp, url}
} else {
panic(err)
}
}
func main() {
first := make(chan response)
CDNList := map[string]string{
"http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js": "CloudFlare",
"http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js": "GoogleAPIs",
"http://ajax.aspnetcdn.com/ajas/jQuery/jquery-1.9.1.min.js": "ASPNET",
"http://code.jquery.com/jquery-1.9.1.min.js": "jQuery",
}
for url, name := range CDNList {
fmt.Println("Trying... ", name)
go get(name, url, first)
}
cdn := <-first
fmt.Println("\nFirst CDN: ", cdn.name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment