-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.go
251 lines (226 loc) · 6.34 KB
/
stream.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// see PR: https://github.com/gomodule/redigo/pull/557/files
package redis
import (
"errors"
"time"
"github.com/gomodule/redigo/redis"
)
type FieldEntry struct {
Key string
Value []byte
}
type MessageEntry struct {
ID string
Fields []FieldEntry
}
type StreamEntry struct {
StreamName string
Messages []MessageEntry
}
func FieldEntries(replay interface{}, err error) ([]FieldEntry, error) {
result, err := redis.Values(replay, nil)
if err != nil {
return nil, err
}
if len(result)%2 != 0 {
return nil, errors.New("redigo: Entry expects 2x value field")
}
fields := make([]FieldEntry, len(result)/2)
for i := 0; i < len(result)/2; i++ {
fieldName, err := redis.String(result[i], nil)
if err != nil {
return nil, err
}
fieldValue, err := redis.Bytes(result[i+1], nil)
if err != nil {
return nil, err
}
fields[i] = FieldEntry{
Key: fieldName,
Value: fieldValue,
}
}
return fields, nil
}
// Need the replay is a message entry format.
func MessageEntries(reply interface{}, err error) ([]MessageEntry, error) {
result, err := redis.Values(reply, err)
if err != nil {
return nil, err
}
entries := make([]MessageEntry, len(result))
for i, r := range result {
msg, err := redis.Values(r, nil)
if len(msg) != 2 {
return nil, errors.New("redigo: Entry expects two value of message")
}
msgId, err := redis.String(msg[0], nil)
if err != nil {
return nil, err
}
fields, err := FieldEntries(msg[1], nil)
if err != nil {
return nil, err
}
entries[i] = MessageEntry{
ID: msgId,
Fields: fields,
}
}
return entries, nil
}
func StreamEntries(reply interface{}, err error) ([]StreamEntry, error) {
result, err := redis.Values(reply, err)
if err != nil {
return nil, err
}
entries := make([]StreamEntry, len(result))
for i, r := range result {
stream, err := redis.Values(r, nil)
if len(stream) != 2 {
return nil, errors.New("redigo: Entry expects two value of stream format")
}
streamName, err := redis.String(stream[0], nil)
if err != nil {
return nil, err
}
streamMsgs, err := MessageEntries(stream[1], nil)
if err != nil {
return nil, err
}
entries[i] = StreamEntry{
StreamName: streamName,
Messages: streamMsgs,
}
}
return entries, nil
}
// for stream xadd
// return the system id
func (s *RediStore) XAdd(streamName string, key string, val interface{}, kv ...interface{}) (string, error) {
args := []interface{}{
streamName, // stream_name
"*", // auto id
key, val,
}
if len(kv) > 0 {
args = append(args, kv...)
}
conn := s.Pool.Get()
defer conn.Close()
return redis.String(conn.Do("XADD", args...))
}
// capped the stream size
func (s *RediStore) XTrim(streamName string, maxLen int) (int, error) {
args := []interface{}{
streamName, // stream_name
"MAXLEN", "~", maxLen,
}
conn := s.Pool.Get()
defer conn.Close()
return redis.Int(conn.Do("XTRIM", args...))
}
func (s *RediStore) XLen(streamName string) (int, error) {
args := []interface{}{
streamName, // stream_name
}
conn := s.Pool.Get()
defer conn.Close()
return redis.Int(conn.Do("XLEN", args...))
}
func (s *RediStore) XDel(streamName, msgId string, otherIds ...interface{}) (int, error) {
args := []interface{}{
streamName,
msgId,
}
if len(otherIds) > 0 {
args = append(args, otherIds...)
}
conn := s.Pool.Get()
defer conn.Close()
return redis.Int(conn.Do("XDEL", args...))
}
//// for single read
//func (s *RediStore) XRead(streamName string, limit int, timeout time.Duration) ([]StreamEntry, error) {
// conn := s.Pool.Get()
// defer conn.Close()
// return StreamEntries(conn.Do("XREAD", "BLOCK", int64(timeout/(1000*1000)), "COUNT", limit, "STREAMS", streamName, "0"))
//}
// TODO: confirm the MKSTREAM in which versoin
func (s *RediStore) XCreateGroup(streamName, groupName string) error {
conn := s.Pool.Get()
defer conn.Close()
if _, err := conn.Do("XGROUP", "CREATE", streamName, groupName, 0, "MKSTREAM"); err != nil {
if err.Error() == "BUSYGROUP Consumer Group name already exists" {
return ErrDataExist
}
return err
}
return nil
}
func (s *RediStore) XDestroyGroup(streamName, groupName string) error {
conn := s.Pool.Get()
defer conn.Close()
if _, err := conn.Do("XGROUP", "DESTROY", streamName, groupName); err != nil {
return err
}
return nil
}
func (s *RediStore) XDeleteGroupUser(streamName, groupName, consumerName string) error {
conn := s.Pool.Get()
defer conn.Close()
if _, err := conn.Do("XGROUP", "DELCONSUMER", streamName, groupName, consumerName); err != nil {
return err
}
return nil
}
// for multi-consumer
// the group name is set as the stream name
// Need create first before using.
func (s *RediStore) XReadGroup(streamName, groupName, consumerName string, limit int, timeout time.Duration) ([]StreamEntry, error) {
conn := s.Pool.Get()
defer conn.Close()
entries, err := StreamEntries(conn.Do("XREADGROUP", "GROUP", groupName, consumerName, "COUNT", limit, "STREAMS", streamName, "0"))
if err != nil {
if err != redis.ErrNil {
return nil, err
}
// nodata, goto block
} else {
hasData := false
for _, e := range entries {
if len(e.Messages) > 0 {
hasData = true
}
}
if hasData {
return entries, nil
}
// no message data, goto block
}
// goto the block mode
return StreamEntries(conn.Do("XREADGROUP", "GROUP", groupName, consumerName, "BLOCK", int64(timeout/(1000*1000)), "COUNT", limit, "STREAMS", streamName, ">"))
}
func (s *RediStore) XACK(streamName, groupName, entryId string) (int, error) {
conn := s.Pool.Get()
defer conn.Close()
return redis.Int(conn.Do("XACK", streamName, groupName, entryId))
}
// get the pending task.
// Since version 6.2 it is possible to filter entries by their idle-time,
func (s *RediStore) XPending(streamName, groupName string, limit int64) ([]interface{}, error) {
conn := s.Pool.Get()
defer conn.Close()
// TODO:[id:val[0];consumerName:val[1];process duration(ms):val[2];try times:val[3]]
return redis.Values(conn.Do("XPENDING", streamName, groupName, "-", "+", limit))
}
// transfer the timeout task to another consumer.
// XAUTOCLAIM Available since 6.2.0.
func (s *RediStore) XClaim(streamName, groupName, entryId, toConsumerName string, overDuration time.Duration) error {
conn := s.Pool.Get()
defer conn.Close()
if _, err := conn.Do("XCLAIM", streamName, groupName, toConsumerName, int64(overDuration/(1000*1000)), entryId); err != nil {
return err
}
return nil
}