/
service_dispatcher.go
234 lines (193 loc) · 6.86 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
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
package processor
import (
"context"
"fmt"
"git.golaxy.org/core/service"
"git.golaxy.org/core/util/generic"
"git.golaxy.org/core/util/types"
"git.golaxy.org/framework/net/gap"
"git.golaxy.org/framework/net/gap/variant"
"git.golaxy.org/framework/plugins/dserv"
"git.golaxy.org/framework/plugins/log"
"git.golaxy.org/framework/plugins/rpc/callpath"
"git.golaxy.org/framework/util/concurrent"
"reflect"
)
// NewServiceDispatcher 创建分布式服务间的RPC分发器
func NewServiceDispatcher() IDispatcher {
return &_ServiceDispatcher{}
}
// _ServiceDispatcher 分布式服务间的RPC分发器
type _ServiceDispatcher struct {
servCtx service.Context
dist dserv.IDistService
watcher dserv.IWatcher
}
// Init 初始化
func (d *_ServiceDispatcher) Init(ctx service.Context) {
d.servCtx = ctx
d.dist = dserv.Using(ctx)
d.watcher = d.dist.WatchMsg(context.Background(), generic.MakeDelegateFunc2(d.handleMsg))
log.Debugf(d.servCtx, "rpc dispatcher %q started", types.AnyFullName(*d))
}
// Shut 结束
func (d *_ServiceDispatcher) Shut(ctx service.Context) {
<-d.watcher.Terminate()
log.Debugf(d.servCtx, "rpc dispatcher %q stopped", types.AnyFullName(*d))
}
func (d *_ServiceDispatcher) handleMsg(topic string, mp gap.MsgPacket) error {
// 只支持服务域通信
if !d.dist.GetNodeDetails().InDomain(mp.Head.Src) {
return nil
}
switch mp.Head.MsgId {
case gap.MsgId_OneWayRPC:
return d.acceptNotify(mp.Msg.(*gap.MsgOneWayRPC))
case gap.MsgId_RPC_Request:
return d.acceptRequest(mp.Head.Src, mp.Msg.(*gap.MsgRPCRequest))
case gap.MsgId_RPC_Reply:
return d.resolve(mp.Msg.(*gap.MsgRPCReply))
}
return nil
}
func (d *_ServiceDispatcher) 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(d.servCtx, cp.Plugin, cp.Method, req.Args); err != nil {
log.Errorf(d.servCtx, "rpc notify service plugin:%q, method:%q calls failed, %s", cp.Plugin, cp.Method, err)
} else {
log.Debugf(d.servCtx, "rpc notify service plugin:%q, method:%q calls finished", cp.Plugin, cp.Method)
}
return
}()
return nil
case callpath.Runtime:
asyncRet, err := CallRuntime(d.servCtx, cp.EntityId, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(d.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(d.servCtx)
if !ret.OK() {
log.Errorf(d.servCtx, "rpc notify entity:%q, runtime plugin:%q, method:%q calls failed, %s", cp.EntityId, cp.Plugin, cp.Method, ret.Error)
} else {
log.Debugf(d.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(d.servCtx, cp.EntityId, cp.Component, cp.Method, req.Args)
if err != nil {
log.Errorf(d.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(d.servCtx)
if !ret.OK() {
log.Errorf(d.servCtx, "rpc notify entity:%q, component:%q, method:%q calls failed, %s", cp.EntityId, cp.Component, cp.Method, ret.Error)
} else {
log.Debugf(d.servCtx, "rpc notify entity:%q, component:%q, method:%q calls finished", cp.EntityId, cp.Component, cp.Method)
}
}()
return nil
}
return nil
}
func (d *_ServiceDispatcher) 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 d.reply(src, req.CorrId, nil, err)
return err
}
switch cp.Category {
case callpath.Service:
go func() {
retsRV, err := CallService(d.servCtx, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(d.servCtx, "rpc request(%d) service plugin:%q, method:%q calls failed, %s", req.CorrId, cp.Plugin, cp.Method, err)
} else {
log.Debugf(d.servCtx, "rpc request(%d) service plugin:%q, method:%q calls finished", req.CorrId, cp.Plugin, cp.Method)
}
d.reply(src, req.CorrId, retsRV, err)
}()
return nil
case callpath.Runtime:
asyncRet, err := CallRuntime(d.servCtx, cp.EntityId, cp.Plugin, cp.Method, req.Args)
if err != nil {
log.Errorf(d.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls failed, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, err)
go d.reply(src, req.CorrId, nil, err)
return nil
}
go func() {
ret := asyncRet.Wait(d.servCtx)
if !ret.OK() {
log.Errorf(d.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls failed, %s", req.CorrId, cp.EntityId, cp.Plugin, cp.Method, ret.Error)
d.reply(src, req.CorrId, nil, ret.Error)
} else {
log.Debugf(d.servCtx, "rpc request(%d) entity:%q, runtime plugin:%q, method:%q calls finished", req.CorrId, cp.EntityId, cp.Plugin, cp.Method)
d.reply(src, req.CorrId, ret.Value.([]reflect.Value), nil)
}
}()
return nil
case callpath.Entity:
asyncRet, err := CallEntity(d.servCtx, cp.EntityId, cp.Component, cp.Method, req.Args)
if err != nil {
log.Errorf(d.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls failed, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, err)
go d.reply(src, req.CorrId, nil, err)
return nil
}
go func() {
ret := asyncRet.Wait(d.servCtx)
if !ret.OK() {
log.Errorf(d.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls failed, %s", req.CorrId, cp.EntityId, cp.Component, cp.Method, ret.Error)
d.reply(src, req.CorrId, nil, ret.Error)
} else {
log.Debugf(d.servCtx, "rpc request(%d) entity:%q, component:%q, method:%q calls finished", req.CorrId, cp.EntityId, cp.Component, cp.Method)
d.reply(src, req.CorrId, ret.Value.([]reflect.Value), nil)
}
}()
return nil
}
return nil
}
func (d *_ServiceDispatcher) 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(d.servCtx, "rpc reply(%d) to src:%q failed, %s", corrId, src, err)
return
}
if retErr != nil {
msg.Error = *variant.MakeError(retErr)
}
err = d.dist.SendMsg(src, msg)
if err != nil {
log.Errorf(d.servCtx, "rpc reply(%d) to src:%q failed, %s", corrId, src, err)
return
}
log.Debugf(d.servCtx, "rpc reply(%d) to src:%q ok", corrId, src)
}
func (d *_ServiceDispatcher) 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 d.dist.GetFutures().Resolve(reply.CorrId, ret)
}