-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmessage.go
More file actions
77 lines (66 loc) · 1.42 KB
/
message.go
File metadata and controls
77 lines (66 loc) · 1.42 KB
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
package outbox
import (
"encoding/base64"
"encoding/json"
"strconv"
"time"
"github.com/nilorg/eventbus"
)
const (
// MessageVersion 版本
MessageVersion = "v1"
)
// Message 消息
type Message eventbus.Message
// IsCallback 存在回调
func (m *Message) IsCallback() bool {
_, ok := m.Header[MessageHeaderMsgCallbackKey]
return ok
}
// Callback 回调地址
func (m *Message) Callback() string {
callback := m.Header[MessageHeaderMsgCallbackKey]
return callback
}
// IsTimeout 是否超时
func (m *Message) IsTimeout(timeout time.Duration) bool {
v, ok := m.Header[MessageHeaderMsgSendTimeKey]
if !ok {
return true
}
utc, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return true
}
t := time.Unix(utc, 0).Add(timeout)
u := time.Now()
return t.Before(u)
}
// IsID 是否存在ID
func (m *Message) IsID() bool {
_, ok := m.Header[MessageHeaderMsgIDKey]
return ok
}
// ID msg id
func (m *Message) ID() string {
id := m.Header[MessageHeaderMsgIDKey]
return id
}
// EncodeValue 对值进行编码
func EncodeValue(v interface{}) (s string, err error) {
var bytes []byte
if bytes, err = json.Marshal(v); err != nil {
return
}
s = base64.StdEncoding.EncodeToString(bytes)
return
}
// DecodeValue 对值进行解码
func DecodeValue(s string, v interface{}) (err error) {
var bytes []byte
if bytes, err = base64.StdEncoding.DecodeString(s); err != nil {
return
}
err = json.Unmarshal(bytes, v)
return
}