-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcontrols_test.go
70 lines (53 loc) · 1.47 KB
/
controls_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
package queue
import (
"context"
"fmt"
"time"
"k8s.io/client-go/util/workqueue"
"github.com/authzed/controller-idioms/handler"
)
func ExampleNewOperations() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// queue has an object in it
queue.Add("current_key")
key, _ := queue.Get()
// operations are per-key
operations := NewOperations(func() {
queue.Done(key)
}, func(duration time.Duration) {
queue.AddAfter(key, duration)
}, cancel)
// typically called from a handler
handler.NewHandlerFromFunc(func(_ context.Context) {
// do some work
operations.Done()
}, "example").Handle(ctx)
fmt.Println(queue.Len())
operations.Requeue()
fmt.Println(queue.Len())
// Output: 0
// 1
}
func ExampleNewQueueOperationsCtx() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
queue := workqueue.NewRateLimitingQueue(workqueue.DefaultControllerRateLimiter())
// queue has an object in it
queue.Add("current_key")
key, _ := queue.Get()
// operations are per-key
CtxQueue := NewQueueOperationsCtx().WithValue(ctx, NewOperations(func() {
queue.Done(key)
}, func(duration time.Duration) {
queue.AddAfter(key, duration)
}, cancel))
// queue controls are passed via context
handler.NewHandlerFromFunc(func(_ context.Context) {
// do some work
CtxQueue.Done()
}, "example").Handle(ctx)
fmt.Println(queue.Len())
// Output: 0
}