-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
43 lines (36 loc) · 1012 Bytes
/
example_test.go
File metadata and controls
43 lines (36 loc) · 1012 Bytes
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
package bwlimit_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"time"
"github.com/linkdata/bwlimit"
)
func ExampleLimiter_NewLimiter() {
// create a test server
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world!"))
}))
defer srv.Close()
// limit reads to 100 bytes/sec, unlimited writes
lim := bwlimit.NewLimiter(100, 0)
defer lim.Stop()
// Clone the default transport so we avoid global side effects.
tp := http.DefaultTransport.(*http.Transport).Clone()
tp.DialContext = lim.Wrap(nil).DialContext
client := &http.Client{Transport: tp}
// make a request and time it
now := time.Now()
resp, err := client.Get(srv.URL)
elapsed := time.Since(now)
if err == nil {
defer resp.Body.Close()
var body []byte
if body, err = io.ReadAll(resp.Body); err == nil {
fmt.Printf("%v %v %q\n", elapsed >= time.Second, lim.Reads.Count.Load() > 100, string(body))
}
}
// Output:
// true true "Hello world!"
}