• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

benchmarks/H27-Jan-2019-

sample/H27-Jan-2019-

LICENSE.mdH A D27-Jan-20191 KiB

README.mdH A D27-Jan-20194.4 KiB

pester.goH A D27-Jan-201912.7 KiB

pester_test.goH A D27-Jan-201917.7 KiB

README.md

1# pester
2
3`pester` wraps Go's standard lib http client to provide several options to increase resiliency in your request. If you experience poor network conditions or requests could experience varied delays, you can now pester the endpoint for data.
4- Send out multiple requests and get the first back (only used for GET calls)
5- Retry on errors
6- Backoff
7
8### Simple Example
9Use `pester` where you would use the http client calls. By default, pester will use a concurrency of 1, and retry the endpoint 3 times with the `DefaultBackoff` strategy of waiting 1 second between retries.
10```go
11/* swap in replacement, just switch
12   http.{Get|Post|PostForm|Head|Do} to
13   pester.{Get|Post|PostForm|Head|Do}
14*/
15resp, err := pester.Get("http://sethammons.com")
16```
17
18### Backoff Strategy
19Provide your own backoff strategy, or use one of the provided built in strategies:
20- `DefaultBackoff`: 1 second
21- `LinearBackoff`: n seconds where n is the retry number
22- `LinearJitterBackoff`: n seconds where n is the retry number, +/- 0-33%
23- `ExponentialBackoff`: n seconds where n is 2^(retry number)
24- `ExponentialJitterBackoff`: n seconds where n is 2^(retry number), +/- 0-33%
25
26```go
27client := pester.New()
28client.Backoff = func(retry int) time.Duration {
29    // set up something dynamic or use a look up table
30    return time.Duration(retry) * time.Minute
31}
32```
33
34### Complete example
35For a complete and working example, see the sample directory.
36`pester` allows you to use a constructor to control:
37- backoff strategy
38- retries
39- concurrency
40- keeping a log for debugging
41```go
42package main
43
44import (
45    "log"
46    "net/http"
47    "strings"
48
49    "github.com/sethgrid/pester"
50)
51
52func main() {
53    log.Println("Starting...")
54
55    { // drop in replacement for http.Get and other client methods
56        resp, err := pester.Get("http://example.com")
57        if err != nil {
58            log.Println("error GETing example.com", err)
59        }
60        defer resp.Body.Close()
61        log.Printf("example.com %s", resp.Status)
62    }
63
64    { // control the resiliency
65        client := pester.New()
66        client.Concurrency = 3
67        client.MaxRetries = 5
68        client.Backoff = pester.ExponentialBackoff
69        client.KeepLog = true
70
71        resp, err := client.Get("http://example.com")
72        if err != nil {
73            log.Println("error GETing example.com", client.LogString())
74        }
75        defer resp.Body.Close()
76        log.Printf("example.com %s", resp.Status)
77    }
78
79    { // use the pester version of http.Client.Do
80        req, err := http.NewRequest("POST", "http://example.com", strings.NewReader("data"))
81        if err != nil {
82            log.Fatal("Unable to create a new http request", err)
83        }
84        resp, err := pester.Do(req)
85        if err != nil {
86            log.Println("error POSTing example.com", err)
87        }
88        defer resp.Body.Close()
89        log.Printf("example.com %s", resp.Status)
90    }
91}
92
93```
94
95### Example Log
96`pester` also allows you to control the resiliency and can optionally log the errors.
97```go
98c := pester.New()
99c.KeepLog = true
100
101nonExistantURL := "http://localhost:9000/foo"
102_, _ = c.Get(nonExistantURL)
103
104fmt.Println(c.LogString())
105/*
106Output:
107
1081432402837 Get [GET] http://localhost:9000/foo request-0 retry-0 error: Get http://localhost:9000/foo: dial tcp 127.0.0.1:9000: connection refused
1091432402838 Get [GET] http://localhost:9000/foo request-0 retry-1 error: Get http://localhost:9000/foo: dial tcp 127.0.0.1:9000: connection refused
1101432402839 Get [GET] http://localhost:9000/foo request-0 retry-2 error: Get http://localhost:9000/foo: dial tcp 127.0.0.1:9000: connection refused
111*/
112```
113
114### Tests
115
116You can run tests in the root directory with `$ go test`. There is a benchmark-like test available with `$ cd benchmarks; go test`.
117You can see `pester` in action with `$ cd sample; go run main.go`.
118
119For watching open file descriptors, you can run `watch "lsof -i -P | grep main"` if you started the app with `go run main.go`.
120I did this for watching for FD leaks. My method was to alter `sample/main.go` to only run one case (`pester.Get with set backoff stategy, concurrency and retries increased`)
121and adding a sleep after the result came back. This let me verify if FDs were getting left open when they should have closed. If you know a better way, let me know!
122I was able to see that FDs are now closing when they should :)
123
124![Are we there yet?](http://butchbellah.com/wp-content/uploads/2012/06/Are-We-There-Yet.jpg)
125
126Are we there yet? Are we there yet? Are we there yet? Are we there yet? ...
127