forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.go
370 lines (311 loc) ยท 9.52 KB
/
plugin.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
package server
import (
"context"
"net"
"github.com/ssdev-go/rpcx/errors"
"github.com/ssdev-go/rpcx/protocol"
)
//PluginContainer represents a plugin container that defines all methods to manage plugins.
//And it also defines all extension points.
type PluginContainer interface {
Add(plugin Plugin)
Remove(plugin Plugin)
All() []Plugin
DoRegister(name string, rcvr interface{}, metadata string) error
DoRegisterFunction(serviceName, fname string, fn interface{}, metadata string) error
DoUnregister(name string) error
DoPostConnAccept(net.Conn) (net.Conn, bool)
DoPostConnClose(net.Conn) bool
DoPreReadRequest(ctx context.Context) error
DoPostReadRequest(ctx context.Context, r *protocol.Message, e error) error
DoPreHandleRequest(ctx context.Context, req *protocol.Message) error
DoPreCall(ctx context.Context, serviceName, methodName string, args interface{}) (interface{}, error)
DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error)
DoPreWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
DoPostWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
DoPreWriteRequest(ctx context.Context) error
DoPostWriteRequest(ctx context.Context, r *protocol.Message, e error) error
DoHeartbeatRequest(ctx context.Context, req *protocol.Message) error
}
// Plugin is the server plugin interface.
type Plugin interface{}
type (
// RegisterPlugin is .
RegisterPlugin interface {
Register(name string, rcvr interface{}, metadata string) error
Unregister(name string) error
}
// RegisterFunctionPlugin is .
RegisterFunctionPlugin interface {
RegisterFunction(serviceName, fname string, fn interface{}, metadata string) error
}
// PostConnAcceptPlugin represents connection accept plugin.
// if returns false, it means subsequent IPostConnAcceptPlugins should not continue to handle this conn
// and this conn has been closed.
PostConnAcceptPlugin interface {
HandleConnAccept(net.Conn) (net.Conn, bool)
}
// PostConnClosePlugin represents client connection close plugin.
PostConnClosePlugin interface {
HandleConnClose(net.Conn) bool
}
// PreReadRequestPlugin represents .
PreReadRequestPlugin interface {
PreReadRequest(ctx context.Context) error
}
// PostReadRequestPlugin represents .
PostReadRequestPlugin interface {
PostReadRequest(ctx context.Context, r *protocol.Message, e error) error
}
// PreHandleRequestPlugin represents .
PreHandleRequestPlugin interface {
PreHandleRequest(ctx context.Context, r *protocol.Message) error
}
PreCallPlugin interface {
PreCall(ctx context.Context, serviceName, methodName string, args interface{}) (interface{}, error)
}
PostCallPlugin interface {
PostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error)
}
// PreWriteResponsePlugin represents .
PreWriteResponsePlugin interface {
PreWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
}
// PostWriteResponsePlugin represents .
PostWriteResponsePlugin interface {
PostWriteResponse(context.Context, *protocol.Message, *protocol.Message, error) error
}
// PreWriteRequestPlugin represents .
PreWriteRequestPlugin interface {
PreWriteRequest(ctx context.Context) error
}
// PostWriteRequestPlugin represents .
PostWriteRequestPlugin interface {
PostWriteRequest(ctx context.Context, r *protocol.Message, e error) error
}
// HeartbeatPlugin is .
HeartbeatPlugin interface {
HeartbeatRequest(ctx context.Context, req *protocol.Message) error
}
)
// pluginContainer implements PluginContainer interface.
type pluginContainer struct {
plugins []Plugin
}
// Add adds a plugin.
func (p *pluginContainer) Add(plugin Plugin) {
p.plugins = append(p.plugins, plugin)
}
// Remove removes a plugin by it's name.
func (p *pluginContainer) Remove(plugin Plugin) {
if p.plugins == nil {
return
}
plugins := make([]Plugin, 0, len(p.plugins))
for _, p := range p.plugins {
if p != plugin {
plugins = append(plugins, p)
}
}
p.plugins = plugins
}
func (p *pluginContainer) All() []Plugin {
return p.plugins
}
// DoRegister invokes DoRegister plugin.
func (p *pluginContainer) DoRegister(name string, rcvr interface{}, metadata string) error {
var es []error
for _, rp := range p.plugins {
if plugin, ok := rp.(RegisterPlugin); ok {
err := plugin.Register(name, rcvr, metadata)
if err != nil {
es = append(es, err)
}
}
}
if len(es) > 0 {
return errors.NewMultiError(es)
}
return nil
}
// DoRegisterFunction invokes DoRegisterFunction plugin.
func (p *pluginContainer) DoRegisterFunction(serviceName, fname string, fn interface{}, metadata string) error {
var es []error
for _, rp := range p.plugins {
if plugin, ok := rp.(RegisterFunctionPlugin); ok {
err := plugin.RegisterFunction(serviceName, fname, fn, metadata)
if err != nil {
es = append(es, err)
}
}
}
if len(es) > 0 {
return errors.NewMultiError(es)
}
return nil
}
// DoUnregister invokes RegisterPlugin.
func (p *pluginContainer) DoUnregister(name string) error {
var es []error
for _, rp := range p.plugins {
if plugin, ok := rp.(RegisterPlugin); ok {
err := plugin.Unregister(name)
if err != nil {
es = append(es, err)
}
}
}
if len(es) > 0 {
return errors.NewMultiError(es)
}
return nil
}
//DoPostConnAccept handles accepted conn
func (p *pluginContainer) DoPostConnAccept(conn net.Conn) (net.Conn, bool) {
var flag bool
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostConnAcceptPlugin); ok {
conn, flag = plugin.HandleConnAccept(conn)
if !flag { //interrupt
conn.Close()
return conn, false
}
}
}
return conn, true
}
//DoPostConnClose handles closed conn
func (p *pluginContainer) DoPostConnClose(conn net.Conn) bool {
var flag bool
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostConnClosePlugin); ok {
flag = plugin.HandleConnClose(conn)
if !flag {
return false
}
}
}
return true
}
// DoPreReadRequest invokes PreReadRequest plugin.
func (p *pluginContainer) DoPreReadRequest(ctx context.Context) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PreReadRequestPlugin); ok {
err := plugin.PreReadRequest(ctx)
if err != nil {
return err
}
}
}
return nil
}
// DoPostReadRequest invokes PostReadRequest plugin.
func (p *pluginContainer) DoPostReadRequest(ctx context.Context, r *protocol.Message, e error) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostReadRequestPlugin); ok {
err := plugin.PostReadRequest(ctx, r, e)
if err != nil {
return err
}
}
}
return nil
}
// DoPreHandleRequest invokes PreHandleRequest plugin.
func (p *pluginContainer) DoPreHandleRequest(ctx context.Context, r *protocol.Message) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PreHandleRequestPlugin); ok {
err := plugin.PreHandleRequest(ctx, r)
if err != nil {
return err
}
}
}
return nil
}
// DoPreCall invokes PreCallPlugin plugin.
func (p *pluginContainer) DoPreCall(ctx context.Context, serviceName, methodName string, args interface{}) (interface{}, error) {
var err error
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PreCallPlugin); ok {
args, err = plugin.PreCall(ctx, serviceName, methodName, args)
if err != nil {
return args, err
}
}
}
return args, err
}
// DoPostCall invokes PostCallPlugin plugin.
func (p *pluginContainer) DoPostCall(ctx context.Context, serviceName, methodName string, args, reply interface{}) (interface{}, error) {
var err error
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostCallPlugin); ok {
reply, err = plugin.PostCall(ctx, serviceName, methodName, args, reply)
if err != nil {
return reply, err
}
}
}
return reply, err
}
// DoPreWriteResponse invokes PreWriteResponse plugin.
func (p *pluginContainer) DoPreWriteResponse(ctx context.Context, req *protocol.Message, res *protocol.Message, err error) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PreWriteResponsePlugin); ok {
err := plugin.PreWriteResponse(ctx, req, res, err)
if err != nil {
return err
}
}
}
return nil
}
// DoPostWriteResponse invokes PostWriteResponse plugin.
func (p *pluginContainer) DoPostWriteResponse(ctx context.Context, req *protocol.Message, resp *protocol.Message, e error) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostWriteResponsePlugin); ok {
err := plugin.PostWriteResponse(ctx, req, resp, e)
if err != nil {
return err
}
}
}
return nil
}
// DoPreWriteRequest invokes PreWriteRequest plugin.
func (p *pluginContainer) DoPreWriteRequest(ctx context.Context) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PreWriteRequestPlugin); ok {
err := plugin.PreWriteRequest(ctx)
if err != nil {
return err
}
}
}
return nil
}
// DoPostWriteRequest invokes PostWriteRequest plugin.
func (p *pluginContainer) DoPostWriteRequest(ctx context.Context, r *protocol.Message, e error) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(PostWriteRequestPlugin); ok {
err := plugin.PostWriteRequest(ctx, r, e)
if err != nil {
return err
}
}
}
return nil
}
// DoHeartbeatRequest invokes HeartbeatRequest plugin.
func (p *pluginContainer) DoHeartbeatRequest(ctx context.Context, r *protocol.Message) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(HeartbeatPlugin); ok {
err := plugin.HeartbeatRequest(ctx, r)
if err != nil {
return err
}
}
}
return nil
}