-
-
Save Hainish/d535e23266295aad45bf5ee6e233a3a3 to your computer and use it in GitHub Desktop.
Ensure we close connections (no KeepAlives)
This file contains hidden or 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" | |
"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