-
Notifications
You must be signed in to change notification settings - Fork 3
/
subscribe.go
217 lines (188 loc) · 5.25 KB
/
subscribe.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
package subscribe
import (
"fmt"
"reflect"
"sync"
)
type subInfo struct {
key string
notifier INotifier
}
// subPub is a wrapper for the publish-subscribe function
type subPub struct {
keyToNotifier sync.Map
subInfoChan chan subInfo
unsubInfoChan chan subInfo
}
type SubPub interface {
// Subscribe a subscription,creates a mapping from nameSpace_kind_param to INotifier
Subscribe(iNotifier INotifier, nameSpace string, kind string, param string) error
// Publish finds the notifier corresponding to the key nameSpace_kind_param, and then calls the Notify method of
// notify to publish message
Publish(nameSpace string, kind string, param string, message interface{}) error
// PublishArray finds the notifier corresponding to the key nameSpace_kind_param, and then calls the Notify method of
// notify to publish a group of message
PublishArray(nameSpace string, kind string, field string, messageList []interface{}) error
}
// NewSubPub return a subPub
func NewSubPub() *subPub {
s := &subPub{
subInfoChan: make(chan subInfo, 50),
unsubInfoChan: make(chan subInfo, 50),
}
go s.process()
return s
}
// Subscribe a subscription, use NewNotifier to warp the *rpc.INotifier and get a INotifier interface
func (s *subPub) Subscribe(iNotifier INotifier, nameSpace string, kind string, param string) error {
var key string
if param != "" {
key = fmt.Sprintf("%s_%s_%s", nameSpace, kind, param)
} else {
key = fmt.Sprintf("%s_%s", nameSpace, kind)
}
info := subInfo{
key: key,
notifier: iNotifier,
}
s.subInfoChan <- info
go func() {
<-iNotifier.Err()
s.unsubInfoChan <- info
}()
return nil
}
// a goroutine to process subscription and unsubscription, start after you call NewSubPub
func (s *subPub) process() {
for {
select {
case info := <-s.subInfoChan:
var slice []*subInfo
v, ok := s.keyToNotifier.Load(info.key)
if !ok {
slice = make([]*subInfo, 0, 1)
} else {
slice = v.([]*subInfo)
}
slice = append(slice, &info)
s.keyToNotifier.Store(info.key, slice)
case info := <-s.unsubInfoChan:
v, ok := s.keyToNotifier.Load(info.key)
if !ok {
continue
}
slice := v.([]*subInfo)
cSlice := make([]*subInfo, len(slice))
copy(cSlice, slice)
for j := 0; j < len(cSlice); j++ {
if cSlice[j].notifier == info.notifier {
cSlice = append(cSlice[:j], cSlice[j+1:]...)
}
}
if len(cSlice) == 0 {
s.keyToNotifier.Delete(info.key)
} else {
s.keyToNotifier.Store(info.key, cSlice)
}
}
}
}
// Publish the message, nameSpace kind param is that you use when you call Subscribe
func (s *subPub) Publish(nameSpace string, kind string, param string, message interface{}) error {
keyPrefix := fmt.Sprintf("%s_%s", nameSpace, kind)
keyList := make([]string, 0, 2)
keyList = append(keyList, keyPrefix)
if param != "" {
keyList = append(keyList, keyPrefix+"_"+param)
}
for i := 0; i < len(keyList); i++ {
v, ok := s.keyToNotifier.Load(keyList[i])
if !ok {
continue
}
slice := v.([]*subInfo)
for _, sub := range slice {
_ = sub.notifier.Notify(keyList[i], message)
}
}
return nil
}
// PublishArray publish a group of message, nameSpace kind is that you use when you call Subscribe.
// the element of messageList should be a struct that has a Field named field. The Field should be
// a string type or implement iString interface so that we can get a string as param.
//
// NOTE:
// type Str1 struct{ str string }
// func (s *Str1) String() string { return s.str }
// *Str1 implements iString interface, but Str1 doesn't.
//
// type Str2 struct{ str string }
// func (s Str2) String() string { return s.str }
// *Str2 implements iString interface, Str2 does also.
func (s *subPub) PublishArray(nameSpace string, kind string, field string, messageList []interface{}) error {
messageMap := make(map[string][]interface{})
keyAll := fmt.Sprintf("%s_%s", nameSpace, kind)
for _, message := range messageList {
param := getKeyInStruct(field, message)
if param != "" {
keySpec := fmt.Sprintf("%s_%s_%s", nameSpace, kind, param)
slice, ok := messageMap[keySpec]
if !ok {
slice = make([]interface{}, 0, 1)
}
slice = append(slice, message)
messageMap[keySpec] = slice
}
slice, ok := messageMap[keyAll]
if !ok {
slice = make([]interface{}, 0, 1)
}
slice = append(slice, message)
messageMap[keyAll] = slice
}
for key, msgList := range messageMap {
v, ok := s.keyToNotifier.Load(key)
if !ok {
continue
}
subList := v.([]*subInfo)
for _, sub := range subList {
for i := 0; i < len(msgList); i++ {
_ = sub.notifier.Notify(key, msgList[i])
}
}
}
return nil
}
// get the param from the field named f in struct s
func getKeyInStruct(f string, s interface{}) string {
value := reflect.ValueOf(s)
var fieldVal reflect.Value
if value.Kind() != reflect.Struct {
if value.Kind() != reflect.Ptr {
return ""
}
v := value.Elem()
if v.Kind() != reflect.Struct {
return ""
}
fieldVal = v.FieldByName(f)
} else {
fieldVal = value.FieldByName(f)
}
if !fieldVal.IsValid() {
return ""
}
if fieldVal.Kind() == reflect.String {
return fieldVal.String()
}
fieldInterface := fieldVal.Interface()
key, ok := fieldInterface.(iString)
if ok {
return key.String()
}
return ""
}
type iString interface {
String() string
}