Skip to content

Instantly share code, notes, and snippets.

@jehiah jehiah/fetchtest.go forked from jsha/fetchtest.go
Last active May 5, 2016

Embed
What would you like to do?
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"net/http"
"os"
"sync"
"time"
)
var parallel = flag.Int("parallel", 5, "parallel requests")
var timeout = flag.Duration("timeout", 2*time.Second, "request timeout")
func noRedirect(req *http.Request, via []*http.Request) error {
return errors.New("stop redirects")
}
func main() {
flag.Parse()
client := http.Client{
CheckRedirect: noRedirect,
Timeout: *timeout,
}
names := make(chan string)
var wg sync.WaitGroup
for i := 0; i < *parallel; i++ {
wg.Add(1)
go func() {
for name := range names {
resp, err := client.Get("https://" + name + "/")
if resp != nil {
fmt.Printf("%s: ok status code %d\n", name, resp.StatusCode)
// we got a redirect response
continue
}
if err != nil {
fmt.Printf("%s: error %s\n", name, err)
} else {
fmt.Printf("%s: ok status code %d\n", name, resp.StatusCode)
}
}
wg.Done()
}()
}
reader := bufio.NewScanner(os.Stdin)
for reader.Scan() {
name := reader.Text()
if name != "" {
names <- name
}
}
close(names)
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.