Browse Source

Parallelize URL checks

renovate/configure
Yota Toyama 8 years ago
parent
commit
be92ded9c1
  1. 46
      main.go

46
main.go

@ -7,6 +7,7 @@ import (
"net/url" "net/url"
"os" "os"
"strings" "strings"
"time"
"github.com/a8m/mark" "github.com/a8m/mark"
"github.com/docopt/docopt-go" "github.com/docopt/docopt-go"
@ -36,17 +37,21 @@ func main() {
panic(err) panic(err)
} }
ok := true checkURLs(extractURLs(n), args["--verbose"].(bool))
}
for s := range extractURLs(n) { func checkURLs(ss []string, verbose bool) {
colored := color.New(color.FgCyan).SprintFunc()(s) client := &http.Client{Timeout: 5 * time.Second}
bs := make(chan bool, len(ss))
if _, err := http.Get(s); err != nil { for _, s := range ss {
printToStderr(color.New(color.FgRed).SprintFunc()("ERROR") + "\t" + colored + "\t" + err.Error()) go checkURL(client, s, bs, verbose)
ok = false
} else if err == nil && args["--verbose"].(bool) {
printToStderr(color.New(color.FgGreen).SprintFunc()("OK") + "\t" + colored)
} }
ok := true
for i := 0; i < len(ss); i++ {
ok = ok && <-bs
} }
if !ok { if !ok {
@ -54,7 +59,20 @@ func main() {
} }
} }
func extractURLs(n *html.Node) map[string]bool { func checkURL(client *http.Client, s string, bs chan bool, verbose bool) {
_, err := client.Get(s)
if s := color.New(color.FgCyan).SprintFunc()(s); err != nil {
printToStderr(
color.New(color.FgRed).SprintFunc()("ERROR") + "\t" + s + "\t" + err.Error())
} else if err == nil && verbose {
printToStderr(color.New(color.FgGreen).SprintFunc()("OK") + "\t" + s)
}
bs <- err == nil
}
func extractURLs(n *html.Node) []string {
ss := make(map[string]bool) ss := make(map[string]bool)
ns := make([]*html.Node, 0, 1024) ns := make([]*html.Node, 0, 1024)
ns = append(ns, n) ns = append(ns, n)
@ -78,6 +96,16 @@ func extractURLs(n *html.Node) map[string]bool {
} }
} }
return stringSetToSlice(ss)
}
func stringSetToSlice(s2b map[string]bool) []string {
ss := make([]string, 0, len(s2b))
for s := range s2b {
ss = append(ss, s)
}
return ss return ss
} }

Loading…
Cancel
Save