-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributed_queue.go
152 lines (141 loc) · 3.85 KB
/
distributed_queue.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package glock
import (
"context"
"encoding/json"
"errors"
"time"
"github.com/go-redis/redis/v8"
)
type IDistributedQueue interface {
// Push to the left
Unshift(key string, values ...interface{}) error
// Push to the right
Push(key string, values ...interface{}) error
// release 1 item right
Pop(key string, out interface{}) error
// release 1 item right
Shift(key string, out interface{}) error
// List item in list
List(key string, start, stop int64, f func([]string) error) error
// Size get current size of list
Size(key string) (int64, error)
}
type DistributedQueue struct {
client *redis.Client
prefix string
timelock time.Duration
}
func ConnectDistributedQueue(client *redis.Client, prefix string, timelock time.Duration) (*DistributedQueue, error) {
if client == nil {
return nil, errors.New("not found client")
}
if timelock < 0 {
return nil, errors.New("timelock is required")
}
ctx, cancel := context.WithTimeout(context.Background(), timelock)
defer cancel()
if err := client.Ping(ctx).Err(); err != nil {
return nil, err
}
return &DistributedQueue{
client: client,
prefix: prefix,
timelock: timelock,
}, nil
}
func (q *DistributedQueue) Set(key string, values ...interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
if err := q.client.Del(ctx, q.prefix+key).Err(); err != nil {
return err
}
if len(values) > 0 {
inputs := []string{}
for _, val := range values {
bin, _ := json.Marshal(val)
inputs = append(inputs, string(bin))
}
if err := q.client.LPush(ctx, q.prefix+key, inputs).Err(); err != nil {
return err
}
}
return nil
}
func (q *DistributedQueue) Push(key string, values ...interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
inputs := []string{}
for _, val := range values {
bin, _ := json.Marshal(val)
inputs = append(inputs, string(bin))
}
if err := q.client.RPush(ctx, q.prefix+key, inputs).Err(); err != nil {
return err
}
return nil
}
func (q *DistributedQueue) Pop(key string, out interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
val, err := q.client.RPop(ctx, q.prefix+key).Result()
if err != nil && err == redis.Nil {
return errors.New(Empty)
}
if err != nil {
return err
}
if err := json.Unmarshal([]byte(val), out); err != nil {
return errors.New(CantParse)
}
return nil
}
func (q *DistributedQueue) Unshift(key string, values ...interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
inputs := []string{}
for _, val := range values {
bin, _ := json.Marshal(val)
inputs = append(inputs, string(bin))
}
if err := q.client.LPush(ctx, q.prefix+key, inputs).Err(); err != nil {
return err
}
return nil
}
func (q *DistributedQueue) Shift(key string, out interface{}) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
val, err := q.client.LPop(ctx, q.prefix+key).Result()
if err != nil && err == redis.Nil {
return errors.New(Empty)
}
if err != nil {
return err
}
if err := json.Unmarshal([]byte(val), out); err != nil {
return errors.New(CantParse)
}
return nil
}
// List start with page 0
func (q *DistributedQueue) List(key string, start, stop int64, f func([]string) error) error {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
vals, err := q.client.LRange(ctx, q.prefix+key, start, stop).Result()
if err != nil && err == redis.Nil {
return errors.New(Empty)
}
if err != nil {
return err
}
return f(vals)
}
func (q *DistributedQueue) Size(key string) (int64, error) {
ctx, cancel := context.WithTimeout(context.Background(), q.timelock)
defer cancel()
c, err := q.client.LLen(ctx, q.prefix+key).Result()
if err != nil {
return 0, err
}
return c, nil
}