-
Notifications
You must be signed in to change notification settings - Fork 411
/
plugin.go
734 lines (688 loc) · 21.6 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Copyright 2015-2019 HenryLee. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package erpc
import (
"fmt"
"net"
"github.com/andeya/goutil"
"github.com/andeya/goutil/errors"
)
// Plug-ins during runtime
type (
// Plugin plugin background
Plugin interface {
Name() string
}
// PreNewPeerPlugin is executed before creating peer.
PreNewPeerPlugin interface {
Plugin
PreNewPeer(*PeerConfig, *PluginContainer) error
}
// PostNewPeerPlugin is executed after creating peer.
PostNewPeerPlugin interface {
Plugin
PostNewPeer(EarlyPeer) error
}
// PostRegPlugin is executed after registering handler.
PostRegPlugin interface {
Plugin
PostReg(*Handler) error
}
// PostListenPlugin is executed between listening and accepting.
PostListenPlugin interface {
Plugin
PostListen(net.Addr) error
}
// PreDialPlugin is executed before dialing.
PreDialPlugin interface {
Plugin
PreDial(localAddr net.Addr, remoteAddr string) *Status
}
// PostDialPlugin is executed after dialing.
PostDialPlugin interface {
Plugin
PostDial(sess PreSession, isRedial bool) *Status
}
// PostAcceptPlugin is executed after accepting connection.
PostAcceptPlugin interface {
Plugin
PostAccept(PreSession) *Status
}
// PreWriteCallPlugin is executed before writing CALL message.
PreWriteCallPlugin interface {
Plugin
PreWriteCall(WriteCtx) *Status
}
// PostWriteCallPlugin is executed after successful writing CALL message.
PostWriteCallPlugin interface {
Plugin
PostWriteCall(WriteCtx) *Status
}
// PreWriteReplyPlugin is executed before writing REPLY message.
PreWriteReplyPlugin interface {
Plugin
PreWriteReply(WriteCtx) *Status
}
// PostWriteReplyPlugin is executed after successful writing REPLY message.
PostWriteReplyPlugin interface {
Plugin
PostWriteReply(WriteCtx) *Status
}
// PreWritePushPlugin is executed before writing PUSH message.
PreWritePushPlugin interface {
Plugin
PreWritePush(WriteCtx) *Status
}
// PostWritePushPlugin is executed after successful writing PUSH message.
PostWritePushPlugin interface {
Plugin
PostWritePush(WriteCtx) *Status
}
// PreReadHeaderPlugin is executed before reading message header.
PreReadHeaderPlugin interface {
Plugin
PreReadHeader(PreCtx) error
}
// PostReadCallHeaderPlugin is executed after reading CALL message header.
PostReadCallHeaderPlugin interface {
Plugin
PostReadCallHeader(ReadCtx) *Status
}
// PreReadCallBodyPlugin is executed before reading CALL message body.
PreReadCallBodyPlugin interface {
Plugin
PreReadCallBody(ReadCtx) *Status
}
// PostReadCallBodyPlugin is executed after reading CALL message body.
PostReadCallBodyPlugin interface {
Plugin
PostReadCallBody(ReadCtx) *Status
}
// PostReadPushHeaderPlugin is executed after reading PUSH message header.
PostReadPushHeaderPlugin interface {
Plugin
PostReadPushHeader(ReadCtx) *Status
}
// PreReadPushBodyPlugin is executed before reading PUSH message body.
PreReadPushBodyPlugin interface {
Plugin
PreReadPushBody(ReadCtx) *Status
}
// PostReadPushBodyPlugin is executed after reading PUSH message body.
PostReadPushBodyPlugin interface {
Plugin
PostReadPushBody(ReadCtx) *Status
}
// PostReadReplyHeaderPlugin is executed after reading REPLY message header.
PostReadReplyHeaderPlugin interface {
Plugin
PostReadReplyHeader(ReadCtx) *Status
}
// PreReadReplyBodyPlugin is executed before reading REPLY message body.
PreReadReplyBodyPlugin interface {
Plugin
PreReadReplyBody(ReadCtx) *Status
}
// PostReadReplyBodyPlugin is executed after reading REPLY message body.
PostReadReplyBodyPlugin interface {
Plugin
PostReadReplyBody(ReadCtx) *Status
}
// PostDisconnectPlugin is executed after disconnection.
PostDisconnectPlugin interface {
Plugin
PostDisconnect(BaseSession) *Status
}
)
// PluginContainer a plugin container
type PluginContainer struct {
*pluginSingleContainer
left *pluginSingleContainer
middle *pluginSingleContainer
right *pluginSingleContainer
refreshTree func()
}
// newPluginContainer new a plugin container.
func newPluginContainer() *PluginContainer {
p := &PluginContainer{
pluginSingleContainer: newPluginSingleContainer(),
left: newPluginSingleContainer(),
middle: newPluginSingleContainer(),
right: newPluginSingleContainer(),
}
p.refreshTree = func() { p.refresh() }
return p
}
func (p *PluginContainer) cloneAndAppendMiddle(plugins ...Plugin) *PluginContainer {
middle := newPluginSingleContainer()
middle.plugins = append(p.middle.GetAll(), plugins...)
newPluginContainer := newPluginContainer()
newPluginContainer.middle = middle
newPluginContainer.left = p.left
newPluginContainer.right = p.right
newPluginContainer.refresh()
oldRefreshTree := p.refreshTree
p.refreshTree = func() {
oldRefreshTree()
newPluginContainer.refresh()
}
return newPluginContainer
}
// AppendLeft appends plugins on the left side of the pluginContainer.
func (p *PluginContainer) AppendLeft(plugins ...Plugin) {
p.left.appendLeft(plugins...)
p.refreshTree()
}
// AppendRight appends plugins on the right side of the pluginContainer.
func (p *PluginContainer) AppendRight(plugins ...Plugin) {
p.right.appendRight(plugins...)
p.refreshTree()
}
// Remove removes a plugin by its name.
func (p *PluginContainer) Remove(pluginName string) error {
err := p.pluginSingleContainer.remove(pluginName)
if err != nil {
return err
}
err = p.left.remove(pluginName)
if err != nil {
return err
}
err = p.middle.remove(pluginName)
if err != nil {
return err
}
err = p.right.remove(pluginName)
if err != nil {
return err
}
p.refreshTree()
return nil
}
func (p *PluginContainer) refresh() {
count := len(p.left.plugins) + len(p.middle.plugins) + len(p.right.plugins)
allPlugins := make([]Plugin, count)
copy(allPlugins[0:], p.left.plugins)
copy(allPlugins[0+len(p.left.plugins):], p.middle.plugins)
copy(allPlugins[0+len(p.left.plugins)+len(p.middle.plugins):], p.right.plugins)
m := make(map[string]bool, count)
for _, plugin := range allPlugins {
if plugin == nil {
Fatalf("plugin cannot be nil!")
return
}
if m[plugin.Name()] {
Fatalf("repeat add plugin: %s", plugin.Name())
return
}
m[plugin.Name()] = true
}
p.pluginSingleContainer.plugins = allPlugins
}
// pluginSingleContainer plugins container.
type pluginSingleContainer struct {
plugins []Plugin
}
// newPluginSingleContainer new a plugin container.
func newPluginSingleContainer() *pluginSingleContainer {
return &pluginSingleContainer{
plugins: make([]Plugin, 0),
}
}
// appendLeft appends plugins on the left side of the pluginContainer.
func (p *pluginSingleContainer) appendLeft(plugins ...Plugin) {
if len(plugins) == 0 {
return
}
p.plugins = append(plugins, p.plugins...)
}
// appendRight appends plugins on the right side of the pluginContainer.
func (p *pluginSingleContainer) appendRight(plugins ...Plugin) {
if len(plugins) == 0 {
return
}
p.plugins = append(p.plugins, plugins...)
}
// GetByName returns a plugin instance by its name.
func (p *pluginSingleContainer) GetByName(pluginName string) Plugin {
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 *pluginSingleContainer) GetAll() []Plugin {
return p.plugins
}
// remove removes a plugin by its name.
func (p *pluginSingleContainer) remove(pluginName string) error {
if p.plugins == nil {
return errors.New("no plugins are registered yet")
}
if len(pluginName) == 0 {
// return error: cannot delete an unamed plugin
return errors.New("plugin with an empty name cannot be removed")
}
indexToRemove := -1
for i, plugin := range p.plugins {
if plugin.Name() == pluginName {
indexToRemove = i
break
}
}
if indexToRemove == -1 {
return errors.New("cannot remove a plugin which isn't exists")
}
p.plugins = append(p.plugins[:indexToRemove], p.plugins[indexToRemove+1:]...)
return nil
}
// PreNewPeer executes the defined plugins before creating peer.
func (p *PluginContainer) preNewPeer(peerConfig *PeerConfig) {
var err error
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreNewPeerPlugin); ok {
if err = _plugin.PreNewPeer(peerConfig, p); err != nil {
Fatalf("[PreNewPeerPlugin:%s] %s", plugin.Name(), err.Error())
return
}
}
}
}
// PostNewPeer executes the defined plugins after creating peer.
func (p *pluginSingleContainer) postNewPeer(peer EarlyPeer) {
var err error
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostNewPeerPlugin); ok {
if err = _plugin.PostNewPeer(peer); err != nil {
Fatalf("[PostNewPeerPlugin:%s] %s", plugin.Name(), err.Error())
return
}
}
}
}
// PostReg executes the defined plugins before registering handler.
func (p *pluginSingleContainer) postReg(h *Handler) {
var err error
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostRegPlugin); ok {
if err = _plugin.PostReg(h); err != nil {
Fatalf("[PostRegPlugin:%s] register handler:%s %s, error:%s", plugin.Name(), h.RouterTypeName(), h.Name(), err.Error())
return
}
}
}
}
// PostListen is executed between listening and accepting.
func (p *pluginSingleContainer) postListen(addr net.Addr) {
var err error
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostListenPlugin); ok {
if err = _plugin.PostListen(addr); err != nil {
Fatalf("[PostListenPlugin:%s] network:%s, addr:%s, error:%s", plugin.Name(), addr.Network(), addr.String(), err.Error())
return
}
}
}
return
}
// preDial executes the defined plugins before dialing.
func (p *pluginSingleContainer) preDial(localAddr net.Addr, remoteAddr string) (stat *Status) {
var pluginName string
defer func() {
if p := recover(); p != nil {
Errorf("[PreDialPlugin:%s] network:%s, localAddr%s ,remoteAddr:%s, panic:%v\n%s", pluginName, localAddr.Network(), localAddr.String(), remoteAddr, p, goutil.PanicTrace(2))
stat = statDialFailed.Copy(p)
}
}()
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreDialPlugin); ok {
pluginName = plugin.Name()
if stat = _plugin.PreDial(localAddr, remoteAddr); !stat.OK() {
LazyDebugf(func() string {
return fmt.Sprintf("[PreDialPlugin:%s] network:%s, localAddr%s ,remoteAddr:%s, error:%s",
pluginName, localAddr.Network(), localAddr.String(), remoteAddr, stat.String(),
)
})
return stat
}
}
}
return nil
}
// postDial executes the defined plugins after dialing.
func (p *pluginSingleContainer) postDial(sess PreSession, isRedial bool) (stat *Status) {
var pluginName string
defer func() {
if p := recover(); p != nil {
Errorf("[PostDialPlugin:%s] network:%s, addr:%s, panic:%v\n%s", pluginName, sess.RemoteAddr().Network(), sess.RemoteAddr().String(), p, goutil.PanicTrace(2))
stat = statDialFailed.Copy(p)
}
}()
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostDialPlugin); ok {
pluginName = plugin.Name()
if stat = _plugin.PostDial(sess, isRedial); !stat.OK() {
LazyDebugf(func() string {
return fmt.Sprintf("[PostDialPlugin:%s] network:%s, addr:%s, is_redial:%v, error:%s",
pluginName, sess.RemoteAddr().Network(), sess.RemoteAddr().String(), isRedial, stat.String(),
)
})
return stat
}
}
}
return nil
}
// PostAccept executes the defined plugins after accepting connection.
func (p *pluginSingleContainer) postAccept(sess PreSession) (stat *Status) {
var pluginName string
defer func() {
if p := recover(); p != nil {
Errorf("[PostAcceptPlugin:%s] network:%s, addr:%s, panic:%v\n%s", pluginName, sess.RemoteAddr().Network(), sess.RemoteAddr().String(), p, goutil.PanicTrace(2))
stat = statInternalServerError.Copy(p)
}
}()
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostAcceptPlugin); ok {
pluginName = plugin.Name()
if stat = _plugin.PostAccept(sess); !stat.OK() {
LazyDebugf(func() string {
return fmt.Sprintf("[PostAcceptPlugin:%s] network:%s, addr:%s, error:%s", pluginName, sess.RemoteAddr().Network(), sess.RemoteAddr().String(), stat.String())
})
return stat
}
}
}
return nil
}
// PreWriteCall executes the defined plugins before writing CALL message.
func (p *pluginSingleContainer) preWriteCall(ctx WriteCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreWriteCallPlugin); ok {
if stat = _plugin.PreWriteCall(ctx); !stat.OK() {
LazyDebugf(func() string {
return fmt.Sprintf("[PreWriteCallPlugin:%s] %s", plugin.Name(), stat.String())
})
return stat
}
}
}
return nil
}
// PostWriteCall executes the defined plugins after successful writing CALL message.
func (p *pluginSingleContainer) postWriteCall(ctx WriteCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostWriteCallPlugin); ok {
if stat = _plugin.PostWriteCall(ctx); !stat.OK() {
Errorf("[PostWriteCallPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PreWriteReply executes the defined plugins before writing REPLY message.
func (p *pluginSingleContainer) preWriteReply(ctx WriteCtx) {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreWriteReplyPlugin); ok {
if stat = _plugin.PreWriteReply(ctx); !stat.OK() {
Errorf("[PreWriteReplyPlugin:%s] %s", plugin.Name(), stat.String())
return
}
}
}
}
// PostWriteReply executes the defined plugins after successful writing REPLY message.
func (p *pluginSingleContainer) postWriteReply(ctx WriteCtx) {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostWriteReplyPlugin); ok {
if stat = _plugin.PostWriteReply(ctx); !stat.OK() {
Errorf("[PostWriteReplyPlugin:%s] %s", plugin.Name(), stat.String())
return
}
}
}
}
// PreWritePush executes the defined plugins before writing PUSH message.
func (p *pluginSingleContainer) preWritePush(ctx WriteCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreWritePushPlugin); ok {
if stat = _plugin.PreWritePush(ctx); !stat.OK() {
LazyDebugf(func() string {
return fmt.Sprintf("[PreWritePushPlugin:%s] %s", plugin.Name(), stat.String())
})
return stat
}
}
}
return nil
}
// PostWritePush executes the defined plugins after successful writing PUSH message.
func (p *pluginSingleContainer) postWritePush(ctx WriteCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostWritePushPlugin); ok {
if stat = _plugin.PostWritePush(ctx); !stat.OK() {
Errorf("[PostWritePushPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PreReadHeader executes the defined plugins before reading message header.
func (p *pluginSingleContainer) preReadHeader(ctx PreCtx) error {
var err error
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreReadHeaderPlugin); ok {
if err = _plugin.PreReadHeader(ctx); err != nil {
LazyDebugf(func() string {
return fmt.Sprintf("[PreReadHeaderPlugin:%s] disconnected when reading: %s", plugin.Name(), err.Error())
})
return err
}
}
}
return nil
}
// PostReadCallHeader executes the defined plugins after reading CALL message header.
func (p *pluginSingleContainer) postReadCallHeader(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadCallHeaderPlugin); ok {
if stat = _plugin.PostReadCallHeader(ctx); !stat.OK() {
Errorf("[PostReadCallHeaderPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PreReadCallBody executes the defined plugins before reading CALL message body.
func (p *pluginSingleContainer) preReadCallBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreReadCallBodyPlugin); ok {
if stat = _plugin.PreReadCallBody(ctx); !stat.OK() {
Errorf("[PreReadCallBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostReadCallBody executes the defined plugins after reading CALL message body.
func (p *pluginSingleContainer) postReadCallBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadCallBodyPlugin); ok {
if stat = _plugin.PostReadCallBody(ctx); !stat.OK() {
Errorf("[PostReadCallBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostReadPushHeader executes the defined plugins after reading PUSH message header.
func (p *pluginSingleContainer) postReadPushHeader(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadPushHeaderPlugin); ok {
if stat = _plugin.PostReadPushHeader(ctx); !stat.OK() {
Errorf("[PostReadPushHeaderPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PreReadPushBody executes the defined plugins before reading PUSH message body.
func (p *pluginSingleContainer) preReadPushBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreReadPushBodyPlugin); ok {
if stat = _plugin.PreReadPushBody(ctx); !stat.OK() {
Errorf("[PreReadPushBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostReadPushBody executes the defined plugins after reading PUSH message body.
func (p *pluginSingleContainer) postReadPushBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadPushBodyPlugin); ok {
if stat = _plugin.PostReadPushBody(ctx); !stat.OK() {
Errorf("[PostReadPushBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostReadReplyHeader executes the defined plugins after reading REPLY message header.
func (p *pluginSingleContainer) postReadReplyHeader(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadReplyHeaderPlugin); ok {
if stat = _plugin.PostReadReplyHeader(ctx); !stat.OK() {
Errorf("[PostReadReplyHeaderPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PreReadReplyBody executes the defined plugins before reading REPLY message body.
func (p *pluginSingleContainer) preReadReplyBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PreReadReplyBodyPlugin); ok {
if stat = _plugin.PreReadReplyBody(ctx); !stat.OK() {
Errorf("[PreReadReplyBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostReadReplyBody executes the defined plugins after reading REPLY message body.
func (p *pluginSingleContainer) postReadReplyBody(ctx ReadCtx) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostReadReplyBodyPlugin); ok {
if stat = _plugin.PostReadReplyBody(ctx); !stat.OK() {
Errorf("[PostReadReplyBodyPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
// PostDisconnect executes the defined plugins after disconnection.
func (p *pluginSingleContainer) postDisconnect(sess BaseSession) *Status {
var stat *Status
for _, plugin := range p.plugins {
if _plugin, ok := plugin.(PostDisconnectPlugin); ok {
if stat = _plugin.PostDisconnect(sess); !stat.OK() {
Errorf("[PostDisconnectPlugin:%s] %s", plugin.Name(), stat.String())
return stat
}
}
}
return nil
}
func warnInvalidHandlerHooks(plugin []Plugin) {
for _, p := range plugin {
switch p.(type) {
case PreNewPeerPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PreNewPeerPlugin in router: %s", p.Name())
})
case PostNewPeerPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostNewPeerPlugin in router: %s", p.Name())
})
case PostDialPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostDialPlugin in router: %s", p.Name())
})
case PostAcceptPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostAcceptPlugin in router: %s", p.Name())
})
case PreWriteCallPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PreWriteCallPlugin in router: %s", p.Name())
})
case PostWriteCallPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostWriteCallPlugin in router: %s", p.Name())
})
case PreWritePushPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PreWritePushPlugin in router: %s", p.Name())
})
case PostWritePushPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostWritePushPlugin in router: %s", p.Name())
})
case PreReadHeaderPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PreReadHeaderPlugin in router: %s", p.Name())
})
case PostReadCallHeaderPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostReadCallHeaderPlugin in router: %s", p.Name())
})
case PostReadPushHeaderPlugin:
LazyDebugf(func() string {
return fmt.Sprintf("invalid PostReadPushHeaderPlugin in router: %s", p.Name())
})
}
}
}