-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_client_test.go
95 lines (81 loc) · 1.99 KB
/
example_client_test.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
package grab
import (
"fmt"
"sync"
)
func ExampleClient_Do() {
client := NewClient()
req, err := NewRequest("/tmp", "http://example.com/example.zip")
if err != nil {
panic(err)
}
resp := client.Do(req)
if err := resp.Err(); err != nil {
panic(err)
}
fmt.Println("Download saved to", resp.Filename)
}
// This example uses DoChannel to create a Producer/Consumer model for
// downloading multiple files concurrently. This is similar to how DoBatch uses
// DoChannel under the hood except that it allows the caller to continually send
// new requests until they wish to close the request channel.
func ExampleClient_DoChannel() {
// create a request and a buffered response channel
reqch := make(chan *Request)
respch := make(chan *Response, 10)
// start 4 workers
client := NewClient()
wg := sync.WaitGroup{}
for i := 0; i < 4; i++ {
wg.Add(1)
go func() {
client.DoChannel(reqch, respch)
wg.Done()
}()
}
go func() {
// send requests
for i := 0; i < 10; i++ {
url := fmt.Sprintf("http://example.com/example%d.zip", i+1)
req, err := NewRequest("/tmp", url)
if err != nil {
panic(err)
}
reqch <- req
}
close(reqch)
// wait for workers to finish
wg.Wait()
close(respch)
}()
// check each response
for resp := range respch {
// block until complete
if err := resp.Err(); err != nil {
panic(err)
}
fmt.Printf("Downloaded %s to %s\n", resp.Request.URL(), resp.Filename)
}
}
func ExampleClient_DoBatch() {
// create multiple download requests
reqs := make([]*Request, 0)
for i := 0; i < 10; i++ {
url := fmt.Sprintf("http://example.com/example%d.zip", i+1)
req, err := NewRequest("/tmp", url)
if err != nil {
panic(err)
}
reqs = append(reqs, req)
}
// start downloads with 4 workers
client := NewClient()
respch := client.DoBatch(4, reqs...)
// check each response
for resp := range respch {
if err := resp.Err(); err != nil {
panic(err)
}
fmt.Printf("Downloaded %s to %s\n", resp.Request.URL(), resp.Filename)
}
}