-
Notifications
You must be signed in to change notification settings - Fork 1
/
service_dispatcher.go
201 lines (166 loc) · 5.95 KB
/
service_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
package processor
import (
"fmt"
"git.golaxy.org/framework/net/gap"
"git.golaxy.org/framework/net/gap/variant"
"git.golaxy.org/framework/plugins/log"
"git.golaxy.org/framework/plugins/rpc/callpath"
"git.golaxy.org/framework/util/concurrent"
"reflect"
)
func (p *_ServiceProcessor) handleMsg(topic string, mp gap.MsgPacket) error {
// 只支持服务域通信
if !p.dist.GetNodeDetails().InDomain(mp.Head.Src) {
return nil
}
switch mp.Head.MsgId {
case gap.MsgId_OneWayRPC:
return p.acceptNotify(mp.Msg.(*gap.MsgOneWayRPC))
case gap.MsgId_RPC_Request:
return p.acceptRequest(mp.Head.Src, mp.Msg.(*gap.MsgRPCRequest))
case gap.MsgId_RPC_Reply:
return p.resolve(mp.Msg.(*gap.MsgRPCReply))
}
return nil
}
func (p *_ServiceProcessor) acceptNotify(req *gap.MsgOneWayRPC) error {
cp, err := callpath.Parse(req.Path)
if err != nil {
return fmt.Errorf("parse rpc notify path:%q failed, %s", req.Path, err)
}
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, %s", cp.Plugin, cp.Method, err)
} else {
log.Debugf(p.servCtx, "rpc notify service plugin:%q, method:%q calls finished", cp.Plugin, cp.Method)
}
return
}()
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, %s", cp.EntityId, cp.Plugin, cp.Method, 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, %s", cp.EntityId, cp.Plugin, cp.Method, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc notify entity:%q, runtime plugin:%q, method:%q calls finished", cp.EntityId, cp.Plugin, cp.Method)
}
}()
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, %s", cp.EntityId, cp.Component, cp.Method, 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, %s", cp.EntityId, cp.Component, cp.Method, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc notify entity:%q, component:%q, method:%q calls finished", cp.EntityId, cp.Component, cp.Method)
}
}()
return nil
}
return nil
}
func (p *_ServiceProcessor) acceptRequest(src string, req *gap.MsgRPCRequest) error {
cp, err := callpath.Parse(req.Path)
if err != nil {
err = fmt.Errorf("parse rpc request(%d) path %q failed, %s", req.CorrId, req.Path, err)
go p.reply(src, req.CorrId, nil, err)
return err
}
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, %s", req.CorrId, cp.Plugin, cp.Method, err)
} else {
log.Debugf(p.servCtx, "rpc request(%d) service plugin:%q, method:%q calls finished", req.CorrId, cp.Plugin, cp.Method)
}
p.reply(src, req.CorrId, retsRV, err)
}()
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, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, err)
go p.reply(src, 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, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, ret.Error)
p.reply(src, req.CorrId, nil, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls finished", req.CorrId, cp.EntityId, cp.Plugin, cp.Method)
p.reply(src, 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, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, err)
go p.reply(src, 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, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, ret.Error)
p.reply(src, req.CorrId, nil, ret.Error)
} else {
log.Debugf(p.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls finished", req.CorrId, cp.EntityId, cp.Component, cp.Method)
p.reply(src, req.CorrId, ret.Value.([]reflect.Value), nil)
}
}()
return nil
}
return nil
}
func (p *_ServiceProcessor) reply(src 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)
}
err = p.dist.SendMsg(src, msg)
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 *_ServiceProcessor) 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)
}