-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmain.go
73 lines (61 loc) · 1.38 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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/http/httptest"
"net/http/httputil"
"sync"
"github.com/julienschmidt/httprouter"
"github.com/projectdiscovery/retryablehttp-go"
)
var (
url string
short bool
)
func main() {
flag.StringVar(&url, "url", "https://scanme.sh", "URL to fetch")
flag.BoolVar(&short, "short", false, "Skip printing http response body")
flag.Parse()
// close connection after each request
opts := retryablehttp.DefaultOptionsSpraying
// opts := retryablehttp.DefaultOptionsSingle // use single options for single host
client := retryablehttp.NewClient(opts)
resp, err := client.Get(url)
if err != nil {
panic(err)
}
bin, err := httputil.DumpResponse(resp, !short)
if err != nil {
panic(err)
}
fmt.Println(string(bin))
// connection reuse
opts = retryablehttp.DefaultOptionsSingle
client = retryablehttp.NewClient(opts)
router := httprouter.New()
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprintf(w, "this is a test")
})
ts := httptest.NewServer(router)
defer ts.Close()
var wg sync.WaitGroup
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < 20; i++ {
resp, err := client.Get(ts.URL)
if err != nil {
log.Println(err)
continue
}
io.Copy(io.Discard, resp.Body)
resp.Body.Close()
}
}()
}
wg.Wait()
}