forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_ratelimit_test.go
More file actions
45 lines (36 loc) · 835 Bytes
/
example_ratelimit_test.go
File metadata and controls
45 lines (36 loc) · 835 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
44
45
package taskq_test
import (
"context"
"fmt"
"time"
"github.com/vmihailenco/taskq/v3"
"github.com/vmihailenco/taskq/v3/memqueue"
)
type RateLimitError string
func (e RateLimitError) Error() string {
return string(e)
}
func (RateLimitError) Delay() time.Duration {
return 3 * time.Second
}
func Example_customRateLimit() {
start := time.Now()
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
})
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "Example_customRateLimit",
Handler: func() error {
fmt.Println("retried in", timeSince(start))
return RateLimitError("calm down")
},
RetryLimit: 2,
MinBackoff: time.Millisecond,
})
ctx := context.Background()
q.Add(task.WithArgs(ctx))
// Wait for all messages to be processed.
_ = q.Close()
// Output: retried in 0s
// retried in 3s
}