Last active
August 29, 2015 14:22
-
-
Save xmunoz/ba20f8c40b7b1f430985 to your computer and use it in GitHub Desktop.
Finding dead links of underwater rugby teams
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 ( | |
"bufio" | |
"fmt" | |
"net/http" | |
"os" | |
"time" | |
) | |
var allowableResponseCodes = map[int]bool{ | |
200: true, | |
301: true, | |
302: true, | |
} | |
// reads a whole file into memory | |
func readLines(path string) ([]string, error) { | |
file, err := os.Open(path) | |
if err != nil { | |
return nil, err | |
} | |
defer file.Close() | |
var lines []string | |
scanner := bufio.NewScanner(file) | |
for scanner.Scan() { | |
lines = append(lines, scanner.Text()) | |
} | |
return lines, scanner.Err() | |
} | |
func asynchronousGets(lines []string) { | |
c := make(chan string) | |
n := 0 | |
for _, url := range lines { | |
go asyncGet(url, c) | |
n++ | |
} | |
timeout := time.After(3 * time.Second) | |
for i := 0; i < n; i++ { | |
select { | |
case result := <-c: | |
if result != "" { | |
fmt.Println(i, result) | |
} | |
case <- timeout: | |
fmt.Printf("%d Timeout!\n", i) | |
} | |
} | |
} | |
func asyncGet(url string, c chan <- string) { | |
resp, err := http.Get(url) | |
if err != nil { | |
c <- fmt.Sprintf(" %s, %s", url, err) | |
return | |
} else if allowableResponseCodes[resp.StatusCode] != true { | |
c <- fmt.Sprintf(" %s, %d", url, resp.StatusCode) | |
} else { | |
c <- "" | |
} | |
resp.Body.Close() | |
} | |
func main() { | |
var linksFile string | |
if len(os.Args) == 2 { | |
linksFile = os.Args[1] | |
} else { | |
panic(fmt.Sprintf("Usage: %s <filename>", os.Args[0])) | |
} | |
lines, err := readLines(linksFile) | |
if err != nil { | |
panic(err) | |
} | |
begin := time.Now() | |
asynchronousGets(lines) | |
fmt.Println(time.Since(begin).Seconds()) | |
fmt.Println("Finished!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment