forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
context_5.go
95 lines (80 loc) · 2.48 KB
/
context_5.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
83
84
85
86
87
88
89
90
91
92
93
94
95
// ----------------
// Request/Response
// ----------------
// Sample program that implements a web request with a context that is
// used to timeout the request if it takes too long.
package main
import (
"context"
"io"
"log"
"net"
"net/http"
"os"
"time"
)
func main() {
// Create a new request.
req, err := http.NewRequest("GET", "https://www.ardanlabs.com/blog/post/index.xml", nil)
if err != nil {
log.Println(err)
return
}
// Create a context with a timeout of 50 milliseconds.
ctx, cancel := context.WithTimeout(req.Context(), 50*time.Millisecond)
defer cancel()
// Declare a new transport and client for the call.
tr := http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := http.Client{
Transport: &tr,
}
// Make the web call in a separate Goroutine so it can be cancelled.
ch := make(chan error, 1)
go func() {
log.Println("Starting Request")
// Make the web call and return any error.
// client.Do is going out and trying to hit the request URL. It's probably blocked right
// now because it will need to wait for the entire document to comeback.
resp, err := client.Do(req)
// It the error occurs, we perform a send on the channel to report that we are done. We are
// going to use this channel at some point to report back what is happening.
if err != nil {
ch <- err
return
}
// If it doesn't fail, we close the response body on the return.
defer resp.Body.Close()
// Write the response to stdout.
io.Copy(os.Stdout, resp.Body)
// Then send back the nil instead of error.
ch <- nil
}()
// Wait the request or timeout.
// We perform a receive on ctx.Done saying that we want to wait 50 ms for that whole process
// above to happen. If it doesn't, we signal back to that Goroutine to cancel the sending
// request. We don't have to just walk away and let that eat up resources and finish because we
// are not gonna need it. We are able to call CancelRequest and underneath, we are able to kill
// that connection.
select {
case <-ctx.Done():
log.Println("timeout, cancel work...")
// Cancel the request and wait for it to complete.
tr.CancelRequest(req)
log.Println(<-ch)
case err := <-ch:
if err != nil {
log.Println(err)
}
}
}