Skip to content

Commit

Permalink
Switches to not creating new http client for every request
Browse files Browse the repository at this point in the history
  • Loading branch information
tomnomnom committed Jul 27, 2018
1 parent f77b38d commit 3d09577
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,31 @@ func main() {
var to int
flag.IntVar(&to, "t", 10000, "timeout (milliseconds)")

// verbose flag
var verbose bool
flag.BoolVar(&verbose, "v", false, "output errors to stderr")

flag.Parse()

// make an actual time.Duration out of the timeout
timeout := time.Duration(to * 1000000)

var tr = &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}

client := &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: timeout,
}

// we send urls to check on the urls channel,
// but only get them on the output channel if
// they are accepting connections
Expand All @@ -58,8 +78,13 @@ func main() {

go func() {
for url := range urls {
if isListening(url, timeout) {
if isListening(client, url) {
fmt.Println(url)
continue
}

if verbose {
fmt.Fprintf(os.Stderr, "failed: %s\n", url)
}
}

Expand Down Expand Up @@ -103,22 +128,7 @@ func main() {
wg.Wait()
}

func isListening(url string, timeout time.Duration) bool {
var tr = &http.Transport{
MaxIdleConns: 30,
IdleConnTimeout: time.Second * 30,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

re := func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}

client := &http.Client{
Transport: tr,
CheckRedirect: re,
Timeout: timeout,
}
func isListening(client *http.Client, url string) bool {

req, err := http.NewRequest("GET", url, nil)
if err != nil {
Expand Down

0 comments on commit 3d09577

Please sign in to comment.