forked from vmihailenco/taskq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
127 lines (104 loc) · 2.58 KB
/
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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package taskq_test
import (
"context"
"errors"
"fmt"
"math"
"time"
"github.com/go-redis/redis_rate/v9"
"github.com/vmihailenco/taskq/v3"
"github.com/vmihailenco/taskq/v3/memqueue"
)
func timeSince(start time.Time) time.Duration {
secs := float64(time.Since(start)) / float64(time.Second)
return time.Duration(math.Floor(secs)) * time.Second
}
func timeSinceCeil(start time.Time) time.Duration {
secs := float64(time.Since(start)) / float64(time.Second)
return time.Duration(math.Ceil(secs)) * time.Second
}
func Example_retryOnError() {
start := time.Now()
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
})
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "Example_retryOnError",
Handler: func() error {
fmt.Println("retried in", timeSince(start))
return errors.New("fake error")
},
RetryLimit: 3,
MinBackoff: time.Second,
})
ctx := context.Background()
q.Add(task.WithArgs(ctx))
// Wait for all messages to be processed.
_ = q.Close()
// Output: retried in 0s
// retried in 1s
// retried in 3s
}
func Example_messageDelay() {
start := time.Now()
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
})
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "Example_messageDelay",
Handler: func() {
fmt.Println("processed with delay", timeSince(start))
},
})
ctx := context.Background()
msg := task.WithArgs(ctx)
msg.Delay = time.Second
_ = q.Add(msg)
// Wait for all messages to be processed.
_ = q.Close()
// Output: processed with delay 1s
}
func Example_rateLimit() {
start := time.Now()
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
Redis: redisRing(),
RateLimit: redis_rate.PerSecond(1),
})
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "Example_rateLimit",
Handler: func() {},
})
const n = 5
ctx := context.Background()
for i := 0; i < n; i++ {
_ = q.Add(task.WithArgs(ctx))
}
// Wait for all messages to be processed.
_ = q.Close()
fmt.Printf("%d msg/s", timeSinceCeil(start)/time.Second/n)
// Output: 1 msg/s
}
func Example_once() {
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
Redis: redisRing(),
RateLimit: redis_rate.PerSecond(1),
})
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "Example_once",
Handler: func(name string) {
fmt.Println("hello", name)
},
})
ctx := context.Background()
for i := 0; i < 10; i++ {
msg := task.WithArgs(ctx, "world")
// Call once in a second.
msg.OnceInPeriod(time.Second)
_ = q.Add(msg)
}
// Wait for all messages to be processed.
_ = q.Close()
// Output: hello world
}