/
timeout.go
39 lines (30 loc) · 681 Bytes
/
timeout.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
package main
import (
"fmt"
"net/http"
"time"
"github.com/mozillazg/request"
)
func diff(req *request.Request) {
url := "http://example.com:12345"
start := time.Now()
req.Get(url)
diff := time.Now().Sub(start)
fmt.Println(diff.Seconds())
}
func main() {
c := new(http.Client)
req := request.NewRequest(c)
fmt.Println("default timeout")
diff(req)
timeout := time.Duration(1 * time.Second)
c.Timeout = timeout
fmt.Printf("set timeout = %f seconds\n", timeout.Seconds())
diff(req)
// Or use req.Client
c = new(http.Client)
req = request.NewRequest(c)
req.Client.Timeout = timeout
fmt.Printf("set timeout = %f seconds\n", timeout.Seconds())
diff(req)
}