-
Notifications
You must be signed in to change notification settings - Fork 55
/
events.go
64 lines (59 loc) · 1.83 KB
/
events.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
package delayqueue
import (
"errors"
"strconv"
"strings"
)
const (
// NewMessageEvent emmited when send message
NewMessageEvent = iota + 1
// ReadyEvent emmited when messages has reached delivery time
ReadyEvent
// DeliveredEvent emmited when messages has been delivered to consumer
DeliveredEvent
// AckEvent emmited when receive message successfully consumed callback
AckEvent
// AckEvent emmited when receive message consumption failure callback
NackEvent
// RetryEvent emmited when message re-delivered to consumer
RetryEvent
// FinalFailedEvent emmited when message reaches max retry attempts
FinalFailedEvent
)
// Event contains internal event information during the queue operation and can be used to monitor the queue status.
type Event struct {
// Code represents event type, such as NewMessageEvent, ReadyEvent
Code int
// Timestamp is the event time
Timestamp int64
// MsgCount represents the number of messages related to the event
MsgCount int
}
func encodeEvent(e *Event) string {
return strconv.Itoa(e.Code) +
" " + strconv.FormatInt(e.Timestamp, 10) +
" " + strconv.Itoa(e.MsgCount)
}
func decodeEvent(payload string) (*Event, error) {
items := strings.Split(payload, " ")
if len(items) != 3 {
return nil, errors.New("[decode event error! wrong item count, payload: " + payload)
}
code, err := strconv.Atoi(items[0])
if err != nil {
return nil, errors.New("decode event error! wrong event code, payload: " + payload)
}
timestamp, err := strconv.ParseInt(items[1], 10, 64)
if err != nil {
return nil, errors.New("decode event error! wrong timestamp, payload: " + payload)
}
count, err := strconv.Atoi(items[2])
if err != nil {
return nil, errors.New("decode event error! wrong msg count, payload: " + payload)
}
return &Event{
Code: code,
Timestamp: timestamp,
MsgCount: count,
}, nil
}