-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
48 lines (37 loc) · 890 Bytes
/
example_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
package congestion_test
import (
"context"
"time"
"github.com/joshbohde/congestion"
)
func doRequest() error {
return nil
}
func Example() {
const (
HighPriority = 100
)
// A limiter can be used to manage concurrent access to a rate limited resource
limiter := congestion.New(congestion.Config{
Capacity: 10,
MaxLimit: 10,
})
// backoff manages a single retryable request with priority
backoff := congestion.Backoff{
Step: 100 * time.Millisecond,
Limiter: &limiter,
Priority: HighPriority,
}
defer backoff.Close()
// Try the request until either it succeeds, or the context is canceled
for backoff.Try(context.Background()) {
// Make some sort of request to the rate limited resource
err := doRequest()
// If this error signals we are overloading the server, we'll retry
if err != nil {
continue
}
// Otherwise return
return
}
}