/
subscriber.go
277 lines (241 loc) · 5.75 KB
/
subscriber.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package router
import (
"context"
"fmt"
"hash/crc32"
"reflect"
"strings"
"github.com/volts-dev/utils"
"github.com/volts-dev/volts/registry"
)
type (
SubscriberOption func(*SubscriberConfig)
SubscriberConfig struct {
Context context.Context
Queue string
// AutoAck defaults to true. When a handler returns
// with a nil error the message is acked.
AutoAck bool
Internal bool
}
/*
handler struct {
method reflect.Value
reqType reflect.Type
ctxType reflect.Type
}
*/
subscriber struct {
topic string
rcvr reflect.Value
typ reflect.Type
subscriber interface{} // 原型 subscriber
handlers []*handler
endpoints []*registry.Endpoint
config SubscriberConfig
}
TSubscriberContext struct {
}
// Subscriber interface represents a subscription to a given topic using
// a specific subscriber function or object with endpoints. It mirrors
// the handler in its behavior.
ISubscriber interface {
Topic() string
Subscriber() interface{} // 原型
Endpoints() []*registry.Endpoint
Handlers() []*handler
Config() SubscriberConfig
}
)
func validateSubscriber(sub ISubscriber) error {
return nil
}
func extractValue(v reflect.Type, d int) *registry.Value {
if d == 3 {
return nil
}
if v == nil {
return nil
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
}
arg := ®istry.Value{
Name: v.Name(),
Type: v.Name(),
}
switch v.Kind() {
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
val := extractValue(f.Type, d+1)
if val == nil {
continue
}
// if we can find a json tag use it
if tags := f.Tag.Get("json"); len(tags) > 0 {
parts := strings.Split(tags, ",")
val.Name = parts[0]
}
// if there's no name default it
if len(val.Name) == 0 {
val.Name = v.Field(i).Name
}
arg.Values = append(arg.Values, val)
}
case reflect.Slice:
p := v.Elem()
if p.Kind() == reflect.Ptr {
p = p.Elem()
}
arg.Type = "[]" + p.Name()
val := extractValue(v.Elem(), d+1)
if val != nil {
arg.Values = append(arg.Values, val)
}
}
return arg
}
func extractSubValue(typ reflect.Type) *registry.Value {
var reqType reflect.Type
switch typ.NumIn() {
case 1:
reqType = typ.In(0)
case 2:
reqType = typ.In(1)
case 3:
reqType = typ.In(2)
default:
return nil
}
return extractValue(reqType, 0)
}
func newSubscriber(topic string, sub any, opts ...SubscriberOption) ISubscriber {
cfg := SubscriberConfig{
AutoAck: true,
}
for _, o := range opts {
o(&cfg)
}
handlers, endpoints := parseSubscriber(topic, sub, opts...)
return &subscriber{
rcvr: reflect.ValueOf(sub),
typ: reflect.TypeOf(sub),
topic: topic,
subscriber: sub,
handlers: handlers,
endpoints: endpoints,
config: cfg,
}
}
func parseSubscriber(topic string, sub any, opts ...SubscriberOption) (handlers []*handler, endpoints []*registry.Endpoint) {
var hd *handler
switch v := sub.(type) {
case func(*TSubscriberContext):
hd = &handler{
Id: int(crc32.ChecksumIEEE([]byte(topic))),
Manager: deflautHandlerManager,
Config: &ControllerConfig{},
Type: SubscriberHandler,
pos: -1,
ctrlType: reflect.TypeOf(v),
ctrlValue: reflect.ValueOf(v),
}
hd.TransportType = SubscribeHandler
hd.funcs = append(hd.funcs, &handle{IsFunc: true, SubFunc: v})
handlers = append(handlers, hd)
endpoints = append(endpoints, ®istry.Endpoint{
Name: "Func",
Request: extractSubValue(hd.ctrlType),
Metadata: map[string]string{
"topic": topic,
"subscriber": "true",
},
})
return
default:
// init Value and Type
ctrlValue, ok := sub.(reflect.Value)
if !ok {
ctrlValue = reflect.ValueOf(sub)
}
ctrlType := ctrlValue.Type()
kind := ctrlType.Kind()
switch kind {
case reflect.Struct, reflect.Ptr:
// transfer prt to struct
if kind == reflect.Ptr {
ctrlValue = ctrlValue.Elem()
ctrlType = ctrlType.Elem()
}
// 获取控制器名称
var objName string
if v, ok := sub.(IString); ok {
objName = v.String()
} else {
objName = utils.DotCasedName(utils.Obj2Name(v))
}
var name string
var method reflect.Value
for i := 0; i < ctrlType.NumMethod(); i++ {
// get the method information from the ctrl Type
name = ctrlType.Method(i).Name
method = ctrlType.Method(i).Func
// 忽略非handler方法
if method.Type().NumIn() <= 1 {
continue
}
if method.CanInterface() {
hds, eps := parseSubscriber(objName+"."+name, method, opts...)
handlers = append(handlers, hds...)
endpoints = append(endpoints, eps...)
}
}
// the end of the struct mapping
return
case reflect.Func:
// Method must be exported.
if ctrlType.PkgPath() != "" {
log.Fatalf("Method %s must be exported", v)
return
}
hd = &handler{
Id: int(crc32.ChecksumIEEE([]byte(topic))),
Manager: deflautHandlerManager,
Config: &ControllerConfig{},
Type: SubscriberHandler,
pos: -1,
ctrlType: ctrlValue.Type(),
ctrlValue: ctrlValue,
}
hd.Name = fmt.Sprintf("%s-%d", topic, hd.Id)
handlers = append(handlers, hd)
endpoints = append(endpoints, ®istry.Endpoint{
Name: hd.Name,
Request: extractSubValue(hd.ctrlType),
Metadata: map[string]string{
"topic": topic,
"subscriber": "true",
},
})
default:
log.Fatal("controller must be func or bound method")
}
}
return handlers, endpoints
}
func (s *subscriber) Topic() string {
return s.topic
}
func (s *subscriber) Subscriber() interface{} {
return s.subscriber
}
func (s *subscriber) Endpoints() []*registry.Endpoint {
return s.endpoints
}
func (s *subscriber) Handlers() []*handler {
return s.handlers
}
func (s *subscriber) Config() SubscriberConfig {
return s.config
}