forked from smallnest/rpcx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_plugin.go
272 lines (226 loc) · 6.64 KB
/
server_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
package rpcx
import (
"net"
"net/rpc"
)
// ServerPluginContainer implements IPluginContainer interface.
type ServerPluginContainer struct {
plugins []IPlugin
}
// Add adds a plugin.
func (p *ServerPluginContainer) Add(plugin IPlugin) error {
if p.plugins == nil {
p.plugins = make([]IPlugin, 0)
}
pName := p.GetName(plugin)
if pName != "" && p.GetByName(pName) != nil {
return ErrPluginAlreadyExists.Format(pName)
}
p.plugins = append(p.plugins, plugin)
return nil
}
// Remove removes a plugin by it's name.
func (p *ServerPluginContainer) Remove(pluginName string) error {
if p.plugins == nil {
return ErrPluginRemoveNoPlugins.Return()
}
if pluginName == "" {
//return error: cannot delete an unamed plugin
return ErrPluginRemoveEmptyName.Return()
}
indexToRemove := -1
for i := range p.plugins {
if p.GetName(p.plugins[i]) == pluginName {
indexToRemove = i
}
}
if indexToRemove == -1 {
return ErrPluginRemoveNotFound.Return()
}
p.plugins = append(p.plugins[:indexToRemove], p.plugins[indexToRemove+1:]...)
return nil
}
// GetName returns the name of a plugin, if no GetName() implemented it returns an empty string ""
func (p *ServerPluginContainer) GetName(plugin IPlugin) string {
return plugin.Name()
}
// GetByName returns a plugin instance by it's name
func (p *ServerPluginContainer) GetByName(pluginName string) IPlugin {
if p.plugins == nil {
return nil
}
for _, plugin := range p.plugins {
if plugin.Name() == pluginName {
return plugin
}
}
return nil
}
// GetAll returns all activated plugins
func (p *ServerPluginContainer) GetAll() []IPlugin {
return p.plugins
}
// DoRegister invokes DoRegister plugin.
func (p *ServerPluginContainer) DoRegister(name string, rcvr interface{}, metadata ...string) error {
var errors []error
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IRegisterPlugin); ok {
err := plugin.Register(name, rcvr, metadata...)
if err != nil {
errors = append(errors, err)
}
}
}
if len(errors) > 0 {
return NewMultiError(errors)
}
return nil
}
//DoPostConnAccept handles accepted conn
func (p *ServerPluginContainer) DoPostConnAccept(conn net.Conn) (net.Conn, bool) {
var flag bool
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPostConnAcceptPlugin); ok {
conn, flag = plugin.HandleConnAccept(conn)
if !flag { //interrupt
conn.Close()
return conn, false
}
}
}
return conn, true
}
// DoPreReadRequestHeader invokes DoPreReadRequestHeader plugin.
func (p *ServerPluginContainer) DoPreReadRequestHeader(r *rpc.Request) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPreReadRequestHeaderPlugin); ok {
err := plugin.PreReadRequestHeader(r)
if err != nil {
return err
}
}
}
return nil
}
// DoPostReadRequestHeader invokes DoPostReadRequestHeader plugin.
func (p *ServerPluginContainer) DoPostReadRequestHeader(r *rpc.Request) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPostReadRequestHeaderPlugin); ok {
err := plugin.PostReadRequestHeader(r)
if err != nil {
return err
}
}
}
return nil
}
// DoPreReadRequestBody invokes DoPreReadRequestBody plugin.
func (p *ServerPluginContainer) DoPreReadRequestBody(body interface{}) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPreReadRequestBodyPlugin); ok {
err := plugin.PreReadRequestBody(body)
if err != nil {
return err
}
}
}
return nil
}
// DoPostReadRequestBody invokes DoPostReadRequestBody plugin.
func (p *ServerPluginContainer) DoPostReadRequestBody(body interface{}) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPostReadRequestBodyPlugin); ok {
err := plugin.PostReadRequestBody(body)
if err != nil {
return err
}
}
}
return nil
}
// DoPreWriteResponse invokes DoPreWriteResponse plugin.
func (p *ServerPluginContainer) DoPreWriteResponse(resp *rpc.Response, body interface{}) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPreWriteResponsePlugin); ok {
err := plugin.PreWriteResponse(resp, body)
if err != nil {
return err
}
}
}
return nil
}
// DoPostWriteResponse invokes DoPostWriteResponse plugin.
func (p *ServerPluginContainer) DoPostWriteResponse(resp *rpc.Response, body interface{}) error {
for i := range p.plugins {
if plugin, ok := p.plugins[i].(IPostWriteResponsePlugin); ok {
err := plugin.PostWriteResponse(resp, body)
if err != nil {
return err
}
}
}
return nil
}
type (
//IRegisterPlugin represents register plugin.
IRegisterPlugin interface {
Register(name string, rcvr interface{}, metadata ...string) error
}
//IPostConnAcceptPlugin represents connection accept plugin.
// if returns false, it means subsequent IPostConnAcceptPlugins should not contiune to handle this conn
// and this conn has been closed.
IPostConnAcceptPlugin interface {
HandleConnAccept(net.Conn) (net.Conn, bool)
}
//IServerCodecPlugin represents .
IServerCodecPlugin interface {
IPreReadRequestHeaderPlugin
IPostReadRequestHeaderPlugin
IPreReadRequestBodyPlugin
IPostReadRequestBodyPlugin
IPreWriteResponsePlugin
IPostWriteResponsePlugin
}
//IPreReadRequestHeaderPlugin represents .
IPreReadRequestHeaderPlugin interface {
PreReadRequestHeader(r *rpc.Request) error
}
//IPostReadRequestHeaderPlugin represents .
IPostReadRequestHeaderPlugin interface {
PostReadRequestHeader(r *rpc.Request) error
}
//IPreReadRequestBodyPlugin represents .
IPreReadRequestBodyPlugin interface {
PreReadRequestBody(body interface{}) error
}
//IPostReadRequestBodyPlugin represents .
IPostReadRequestBodyPlugin interface {
PostReadRequestBody(body interface{}) error
}
//IPreWriteResponsePlugin represents .
IPreWriteResponsePlugin interface {
PreWriteResponse(*rpc.Response, interface{}) error
}
//IPostWriteResponsePlugin represents .
IPostWriteResponsePlugin interface {
PostWriteResponse(*rpc.Response, interface{}) error
}
//IServerPluginContainer represents a plugin container that defines all methods to manage plugins.
//And it also defines all extension points.
IServerPluginContainer interface {
Add(plugin IPlugin) error
Remove(pluginName string) error
GetName(plugin IPlugin) string
GetByName(pluginName string) IPlugin
GetAll() []IPlugin
DoRegister(name string, rcvr interface{}, metadata ...string) error
DoPostConnAccept(net.Conn) (net.Conn, bool)
DoPreReadRequestHeader(r *rpc.Request) error
DoPostReadRequestHeader(r *rpc.Request) error
DoPreReadRequestBody(body interface{}) error
DoPostReadRequestBody(body interface{}) error
DoPreWriteResponse(*rpc.Response, interface{}) error
DoPostWriteResponse(*rpc.Response, interface{}) error
}
)