Skip to content

Instantly share code, notes, and snippets.

@Hainish
Forked from jehiah/fetchtest.go
Last active May 5, 2016 20:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hainish/d535e23266295aad45bf5ee6e233a3a3 to your computer and use it in GitHub Desktop.
Save Hainish/d535e23266295aad45bf5ee6e233a3a3 to your computer and use it in GitHub Desktop.
Ensure we close connections (no KeepAlives)
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()
transport := &http.Transport{
DisableKeepAlives: true,
}
client := http.Client{
Transport: transport,
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