Created
February 23, 2016 16:43
-
-
Save gerep/52a64cddf7d572d273f5 to your computer and use it in GitHub Desktop.
Using channels to test the fastest CDN response
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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