/
schema.go
200 lines (166 loc) · 4.1 KB
/
schema.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
package relay
import (
"crypto/rand"
"encoding/base64"
"encoding/json"
"strings"
"sync"
"go.uber.org/zap/zapcore"
)
type MessageType string
const (
Pub MessageType = "pub"
Sub MessageType = "sub"
Ack MessageType = "ack"
Ping MessageType = "ping"
Pong MessageType = "pong"
)
// websocket message
type SocketMessage struct {
Topic string `json:"topic"`
Type MessageType `json:"type"` // pub, sub, ack
Payload string `json:"payload"`
Role string `json:"role"`
Phase string `json:"phase"`
Silent bool `json:"silent"`
client *client `json:"-"`
}
func (sm SocketMessage) MarshalBinary() ([]byte, error) {
return json.Marshal(sm)
}
type RoleType string
const (
Dapp RoleType = "dapp"
Wallet RoleType = "wallet"
Relay RoleType = "relay"
)
type PhaseType string
const (
SessionRequest PhaseType = "sessionRequest"
SessionReceived PhaseType = "sessionReceived"
SessionExpired PhaseType = "sessionExpired"
SessionStart PhaseType = "sessionStart"
SessionSuspended PhaseType = "sessionSuspended"
SessionResumed PhaseType = "sessionResumed"
)
// redis key prefix
const (
// redis message cache
cachedMessagePrefix = "wc:relay:cache:pendingMessages:"
// redis message channels
messageChan = "wc:relay:chan:messages:"
dappNotifyChan = "wc:relay:chan:dappNotify:"
)
func messageChanKey(topic string) string {
return messageChan + topic
}
func dappNotifyChanKey(topic string) string {
return dappNotifyChan + topic
}
// fromDappNotifyChan checks whether the redis notify message is from the notfyDapp channel
func fromDappNotifyChan(channel string) bool {
return strings.HasPrefix(channel, dappNotifyChan)
}
func cachedMessageKey(topic string) string {
return cachedMessagePrefix + topic
}
// TopicClientSet stores topic -> clients relationship
type TopicClientSet struct {
*sync.RWMutex
Data map[string]map[*client]struct{}
}
func NewTopicClientSet() *TopicClientSet {
return &TopicClientSet{
RWMutex: &sync.RWMutex{},
Data: map[string]map[*client]struct{}{},
}
}
func (ts *TopicClientSet) Get(topic string) map[*client]struct{} {
ts.RLock()
defer ts.RUnlock()
return ts.Data[topic]
}
func (ts *TopicClientSet) Set(topic string, c *client) {
ts.Lock()
defer ts.Unlock()
if _, ok := ts.Data[topic]; !ok {
ts.Data[topic] = make(map[*client]struct{})
}
ts.Data[topic][c] = struct{}{}
}
// GetTopicsByClient returns the topics associated with the specified client,
// meanwhile, remove the client from these topics if `clear` is true
// returns the topics the client has associated with
func (ts *TopicClientSet) GetTopicsByClient(c *client, clear bool) []string {
// Write lock in a read func because we may remove the client from the topics
ts.Lock()
defer ts.Unlock()
topics := []string{}
for topic, set := range ts.Data {
if _, ok := set[c]; ok {
topics = append(topics, topic)
}
if clear {
delete(set, c)
}
}
return topics
}
func (ts *TopicClientSet) Unset(topic string, c *client) {
ts.Lock()
defer ts.Unlock()
delete(ts.Data[topic], c)
}
func (ts *TopicClientSet) Len(topic string) int {
ts.RLock()
defer ts.RUnlock()
return len(ts.Data[topic])
}
func (ts *TopicClientSet) Clear(topic string) {
ts.Lock()
defer ts.Unlock()
delete(ts.Data, topic)
}
type TopicSet struct {
*sync.RWMutex
Data map[string]struct{}
}
func NewTopicSet() *TopicSet {
return &TopicSet{
RWMutex: &sync.RWMutex{},
Data: map[string]struct{}{},
}
}
func (tm TopicSet) Get() map[string]struct{} {
tm.Lock()
defer tm.Unlock()
newmap := make(map[string]struct{})
for key, value := range tm.Data {
newmap[key] = value
}
return newmap
}
func (tm TopicSet) Set(topic string) {
tm.Lock()
defer tm.Unlock()
tm.Data[topic] = struct{}{}
}
func (tm TopicSet) MarshalLogArray(encoder zapcore.ArrayEncoder) error {
tm.Lock()
defer tm.Unlock()
for topic := range tm.Data {
encoder.AppendString(topic)
}
return nil
}
type ClientUnregisterEvent struct {
client *client
reason error
}
func generateRandomBytes16() string {
buf := make([]byte, 16)
if _, err := rand.Read(buf); err != nil {
return ""
}
return base64.StdEncoding.EncodeToString(buf)
}