summary refs log tree commit diff
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2020-03-15 01:00:23 +0100
committerLeah Neukirchen <leah@vuxu.org>2020-03-15 01:00:23 +0100
commitf346deef7e770283f17e74739161370445077853 (patch)
tree1b2b59ab4f5fac0e6875a377494fe76620f90ec9
parente5c22ce5e64d2111f4acb4a8d18a1041e98a239b (diff)
downloadhtping-f346deef7e770283f17e74739161370445077853.tar.gz
htping-f346deef7e770283f17e74739161370445077853.tar.xz
htping-f346deef7e770283f17e74739161370445077853.zip
reuse transport, so keepalive actually works
-rw-r--r--htping.go16
1 files changed, 9 insertions, 7 deletions
diff --git a/htping.go b/htping.go
index b4ac426..530514f 100644
--- a/htping.go
+++ b/htping.go
@@ -39,6 +39,8 @@ type transport struct {
 	addr string
 }
 
+var myTransport *transport
+
 func newTransport() *transport {
 	tr := &transport{}
 
@@ -115,8 +117,6 @@ func ping(url string, seq int, results chan result) {
 
 	atomic.AddInt32(&ntotal, 1)
 
-	t := newTransport()
-
 	req, err := http.NewRequest(method, url, nil)
 	if err != nil {
 		fmt.Printf("error=%v\n", err)
@@ -128,12 +128,12 @@ func ping(url string, seq int, results chan result) {
 	}
 
 	trace := &httptrace.ClientTrace{
-		GotConn: t.GotConn,
+		GotConn: myTransport.GotConn,
 	}
 	req = req.WithContext(httptrace.WithClientTrace(req.Context(), trace))
 
 	client := &http.Client{
-		Transport: t,
+		Transport: myTransport,
 		Timeout:   10 * time.Second,
 		CheckRedirect: func(req *http.Request, via []*http.Request) error {
 			return http.ErrUseLastResponse
@@ -152,13 +152,13 @@ func ping(url string, seq int, results chan result) {
 
 	dur := float64(stop.Sub(start)) / float64(time.Second)
 
-	if len(t.msg) > 0 {
-		fmt.Printf("%v\n", t.msg)
+	if len(myTransport.msg) > 0 {
+		fmt.Printf("%v\n", myTransport.msg)
 	}
 
 	fmt.Printf("%d bytes from %v: %s %d seq=%d time=%.3f ms\n",
 		written,
-		t.addr,
+		myTransport.addr,
 		res.Proto,
 		res.StatusCode,
 		seq,
@@ -283,6 +283,8 @@ func main() {
 
 	count := 0
 
+	myTransport = newTransport()
+
 	if *flood {
 	flood_loop:
 		for {