-
Notifications
You must be signed in to change notification settings - Fork 1
/
forward_dispatcher.go
278 lines (234 loc) · 9.19 KB
/
forward_dispatcher.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
278
package processor
import (
"fmt"
"git.golaxy.org/core/util/uid"
"git.golaxy.org/framework/net/gap"
"git.golaxy.org/framework/net/gap/variant"
"git.golaxy.org/framework/plugins/gate"
"git.golaxy.org/framework/plugins/log"
"git.golaxy.org/framework/plugins/rpc/callpath"
"git.golaxy.org/framework/util/concurrent"
"reflect"
)
func (p *_ForwardProcessor) handleMsg(topic string, mp gap.MsgPacket) error {
// 只支持客户端域通信
if !gate.CliDetails.InDomain(mp.Head.Src) {
return nil
}
switch mp.Head.MsgId {
case gap.MsgId_Forward:
return p.acceptForward(mp.Head.Src, mp.Msg.(*gap.MsgForward))
}
return nil
}
func (p *_ForwardProcessor) acceptForward(src string, req *gap.MsgForward) error {
switch req.TransId {
case gap.MsgId_OneWayRPC:
msg := &gap.MsgOneWayRPC{}
if err := gap.Unmarshal(msg, req.TransData); err != nil {
return err
}
return p.acceptNotify(src, req.Dst, req.Transit, msg)
case gap.MsgId_RPC_Request:
msg := &gap.MsgRPCRequest{}
if err := gap.Unmarshal(msg, req.TransData); err != nil {
go p.reply(src, req.Transit, req.CorrId, nil, err)
return err
}
return p.acceptRequest(src, req.Dst, req.Transit, msg)
case gap.MsgId_RPC_Reply:
msg := &gap.MsgRPCReply{}
if err := gap.Unmarshal(msg, req.TransData); err != nil {
return err
}
return p.resolve(msg)
}
return nil
}
func (p *_ForwardProcessor) acceptNotify(src, dst, transit string, req *gap.MsgOneWayRPC) error {
cp, err := p.parseCallPath(dst, req.Path)
if err != nil {
return fmt.Errorf("parse rpc notify failed, src:%q, dst:%q, transit:%q, path:%q, %s", src, dst, transit, req.Path, err)
}
if len(p.permValidator) > 0 {
passed, err := p.permValidator.Invoke(func(passed bool, err error) bool {
return !passed || err != nil
}, src, cp)
if !passed && err == nil {
err = ErrPermissionDenied
}
if err != nil {
log.Errorf(p.servCtx, "rpc notify permission verification failed, src:%q, dst:%q, transit:%q, path:%q, %s", src, dst, transit, req.Path, err)
return nil
}
}
switch cp.Category {
case callpath.Service:
go func() {
if _, err := CallService(p.servCtx, cp.Plugin, cp.Method, req.Args); err != nil {
log.Errorf(p.servCtx, "rpc notify service plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", cp.Plugin, cp.Method, src, dst, transit, req.Path, err)
} else {
log.Debugf(p.servCtx, "rpc notify service plugin:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q", cp.Plugin, cp.Method, src, dst, transit, req.Path)
}
}()
return nil
case callpath.Runtime:
asyncRet, err := CallRuntime(p.servCtx, cp.EntityId, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(p.servCtx, "rpc notify entity:%q, runtime plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path, err)
return nil
}
go func() {
ret := asyncRet.Wait(p.servCtx)
if !ret.OK() {
log.Errorf(p.servCtx, "rpc notify entity:%q, runtime plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc notify entity:%q, runtime plugin:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q,", cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path)
}
}()
return nil
case callpath.Entity:
asyncRet, err := CallEntity(p.servCtx, cp.EntityId, cp.Component, cp.Method, req.Args)
if err != nil {
log.Errorf(p.servCtx, "rpc notify entity:%q, component:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path, err)
return nil
}
go func() {
ret := asyncRet.Wait(p.servCtx)
if !ret.OK() {
log.Errorf(p.servCtx, "rpc notify entity:%q, component:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc notify entity:%q, component:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q", cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path)
}
}()
return nil
}
return nil
}
func (p *_ForwardProcessor) acceptRequest(src, dst, transit string, req *gap.MsgRPCRequest) error {
cp, err := p.parseCallPath(dst, req.Path)
if err != nil {
err = fmt.Errorf("parse rpc request(%d) failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, src, dst, transit, req.Path, err)
go p.reply(src, transit, req.CorrId, nil, err)
return err
}
if len(p.permValidator) > 0 {
passed, err := p.permValidator.Invoke(func(passed bool, err error) bool {
return !passed || err != nil
}, src, cp)
if !passed && err == nil {
err = ErrPermissionDenied
}
if err != nil {
log.Errorf(p.servCtx, "rpc request(%d) permission verification failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, src, dst, transit, req.Path, err)
go p.reply(src, transit, req.CorrId, nil, err)
return nil
}
}
switch cp.Category {
case callpath.Service:
go func() {
retsRV, err := CallService(p.servCtx, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(p.servCtx, "rpc request(%d) service plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, cp.Plugin, cp.Method, src, dst, transit, req.Path, err)
} else {
log.Debugf(p.servCtx, "rpc request(%d) service plugin:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q", req.CorrId, cp.Plugin, cp.Method, src, dst, transit, req.Path)
}
p.reply(src, transit, req.CorrId, retsRV, nil)
}()
return nil
case callpath.Runtime:
asyncRet, err := CallRuntime(p.servCtx, cp.EntityId, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(p.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path, err)
go p.reply(src, transit, req.CorrId, nil, err)
return nil
}
go func() {
ret := asyncRet.Wait(p.servCtx)
if !ret.OK() {
log.Errorf(p.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path, ret.Error)
p.reply(src, transit, req.CorrId, nil, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, src, dst, transit, req.Path)
p.reply(src, transit, req.CorrId, ret.Value.([]reflect.Value), nil)
}
}()
return nil
case callpath.Entity:
asyncRet, err := CallEntity(p.servCtx, cp.EntityId, cp.Component, cp.Method, req.Args)
if err != nil {
log.Errorf(p.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path, err)
go p.reply(src, transit, req.CorrId, nil, err)
return nil
}
go func() {
ret := asyncRet.Wait(p.servCtx)
if !ret.OK() {
log.Errorf(p.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls failed, src:%q, dst:%q, transit:%q, path:%q, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path, ret.Error)
p.reply(src, transit, req.CorrId, nil, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls finished, src:%q, dst:%q, transit:%q, path:%q", req.CorrId, cp.EntityId, cp.Component, cp.Method, src, dst, transit, req.Path)
p.reply(src, transit, req.CorrId, ret.Value.([]reflect.Value), nil)
}
}()
return nil
}
return nil
}
func (p *_ForwardProcessor) reply(src, transit string, corrId int64, retsRV []reflect.Value, retErr error) {
if corrId == 0 {
return
}
msg := &gap.MsgRPCReply{
CorrId: corrId,
}
var err error
msg.Rets, err = variant.MakeArray(retsRV)
if err != nil {
log.Errorf(p.servCtx, "rpc reply(%d) to src:%q failed, %s", corrId, src, err)
return
}
if retErr != nil {
msg.Error = *variant.MakeError(retErr)
}
bs, err := gap.Marshal(msg)
if err != nil {
log.Errorf(p.servCtx, "rpc reply(%d) to src:%q failed, %s", corrId, src, err)
return
}
defer bs.Release()
forwardMsg := &gap.MsgForward{
Dst: src,
CorrId: corrId,
TransId: msg.MsgId(),
TransData: bs.Data(),
}
err = p.dist.SendMsg(transit, forwardMsg)
if err != nil {
log.Errorf(p.servCtx, "rpc reply(%d) to src:%q failed, %s", corrId, src, err)
return
}
log.Debugf(p.servCtx, "rpc reply(%d) to src:%q ok", corrId, src)
}
func (p *_ForwardProcessor) resolve(reply *gap.MsgRPCReply) error {
ret := concurrent.Ret[any]{}
if reply.Error.OK() {
if len(reply.Rets) > 0 {
ret.Value = reply.Rets
}
} else {
ret.Error = &reply.Error
}
return p.dist.GetFutures().Resolve(reply.CorrId, ret)
}
func (p *_ForwardProcessor) parseCallPath(dst, path string) (callpath.CallPath, error) {
cp, err := callpath.Parse(path)
if err != nil {
return callpath.CallPath{}, err
}
if cp.EntityId.IsNil() {
cp.EntityId = uid.From(dst)
}
return cp, nil
}