-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_pull.go
92 lines (83 loc) · 2.54 KB
/
handle_pull.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
package main
import (
pb "esdeath_go/esdeath_proto"
"esdeath_go/miku/persistent"
log "github.com/sirupsen/logrus"
"time"
)
const defHoldTime = 20 * time.Second
func pullReqHandle(pull *pb.DelayMsgPull) (*PulledMsg, error) {
var matchEntry *keyEntity
var matchedKey, matchedValue []byte
// 非法key将会被移除,因为永远无法消费
var illegalKeys [][]byte
// false表示继续遍历,true表示停止遍历
matchFunc := func(k, v []byte) bool {
log.Debugln("Iterate key: ", string(k))
sk, err := newKeyEntityFromBytes(k)
if err != nil {
log.Errorln("build key from bytes: ", err)
illegalKeys = append(illegalKeys, k)
return false
}
// 如果状态不是waitConsume,需要继续遍历
if msgStatus(v[0]) != waitConsume {
log.Debugln("msg status is wait consuming")
return false
}
matchEntry = sk
if sk.delayTime > time.Now().UnixMilli() {
log.Debugln("delay time not reach: ", sk.delayTime)
// 遇到第一个延迟时间未到的消息,停止遍历,等待下次长轮询确定后面也不会有达到延迟时间的消息
return true
}
matchedKey, matchedValue = k, v
return true
}
// 避免遍历不是当前topic的key
prefix := []byte(pull.Topic + keyDelimiter + pull.ConsumerGroup)
rkv.PrefixIterate(prefix, matchFunc)
// 删除非法key,当前执行逻辑不影响消费key的逻辑
go func() {
if len(illegalKeys) > 0 {
errPs := make([]*persistent.KVPair, len(illegalKeys))
for i, errKey := range illegalKeys {
errPs[i] = persistent.NewKPair(string(errKey))
}
if err := rkv.Delete(errPs); err != nil {
log.Errorln("delete err keys err: ", err)
}
}
}()
// 没有任何消息:
//1.当前topic下没有消息 2.当前topic下没有waitConsume状态的消息
if matchEntry == nil {
return nil, nil
}
// 没有找到达到延迟时间的消息
if matchedKey == nil {
return &PulledMsg{DelayTimestamp: matchEntry.delayTime}, nil
}
// 更新当前消息状态为 consuming
matchedValue[0] = consuming.toByte()
message := persistent.NewKVPair(string(matchedKey), string(matchedValue))
if err := rkv.Insert([]*persistent.KVPair{message}); err != nil {
log.Errorln("update consuming status err: ", err)
return nil, err
}
log.Infoln("consume key: ", matchEntry)
resp := &PulledMsg{
MsgId: string(matchedKey),
Topic: matchEntry.topic,
Tag: matchEntry.tag,
Payload: matchedValue[1:],
}
return resp, nil
}
type PulledMsg struct {
MsgId string
Topic string
Tag string
Payload []byte
DelayTimestamp int64
}