-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathmain.go
82 lines (73 loc) · 2.33 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"bufio"
"crypto/tls"
"flag"
"fmt"
"net"
"net/http"
"os"
"sync"
"time"
)
func main() {
concurrencyPtr := flag.Int("t", 8, "Number of threads to utilise. Default is 8.")
timeoutPtr := flag.Int("timeout", 8, "Timeout for each request in seconds. Default is 8 seconds.")
retryPtr := flag.Int("retry", 3, "Number of retries for failed requests. Default is 3.")
retrySleepPtr := flag.Int("retry-sleep", 1, "Sleep duration between retries in seconds. Default is 1 second.")
flag.Parse()
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: (&net.Dialer{Timeout: time.Duration(*timeoutPtr) * time.Second, KeepAlive: 0}).Dial,
TLSHandshakeTimeout: time.Duration(*timeoutPtr) * time.Second,
},
}
numWorkers := *concurrencyPtr
work := make(chan string)
go func() {
s := bufio.NewScanner(os.Stdin)
for s.Scan() {
work <- s.Text()
}
if s.Err() != nil {
fmt.Println("Error reading from stdin:", s.Err())
}
close(work)
}()
wg := &sync.WaitGroup{}
for i := 0; i < numWorkers; i++ {
wg.Add(1)
go doWork(work, client, wg, *retryPtr, *retrySleepPtr)
}
wg.Wait()
}
func doWork(work chan string, client *http.Client, wg *sync.WaitGroup, retries int, retrySleep int) {
defer wg.Done()
for url := range work {
var resp *http.Response
var err error
for attempts := 0; attempts <= retries; attempts++ {
req, reqErr := http.NewRequest("GET", url, nil)
if reqErr != nil {
fmt.Println(999, reqErr, url)
break
}
req.Header.Set("Connection", "close")
resp, err = client.Do(req)
if err == nil || attempts == retries {
if err != nil {
fmt.Println(999, err, url)
}
if resp != nil {
resp.Body.Close()
if err == nil {
fmt.Println(resp.StatusCode, url)
}
}
break
}
time.Sleep(time.Duration(retrySleep) * time.Second)
}
}
}