github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/client/plugin_manager.go (about) 1 package client 2 3 import ( 4 "github.com/nyan233/littlerpc/core/middle/plugin" 5 perror "github.com/nyan233/littlerpc/core/protocol/error" 6 "github.com/nyan233/littlerpc/core/protocol/message" 7 "sync" 8 ) 9 10 type pluginManager struct { 11 ctxPool sync.Pool 12 plugins []plugin.ClientPlugin 13 } 14 15 func newPluginManager(plugins []plugin.ClientPlugin) *pluginManager { 16 return &pluginManager{ 17 ctxPool: sync.Pool{ 18 New: func() interface{} { 19 return new(plugin.Context) 20 }, 21 }, 22 plugins: plugins, 23 } 24 } 25 26 func (p *pluginManager) Size() int { 27 return len(p.plugins) 28 } 29 30 func (p *pluginManager) GetContext() *plugin.Context { 31 return p.ctxPool.Get().(*plugin.Context) 32 } 33 34 func (p *pluginManager) FreeContext(ctx *plugin.Context) { 35 p.ctxPool.Put(ctx) 36 } 37 38 func (p *pluginManager) Request4C(pub *plugin.Context, args []interface{}, msg *message.Message) perror.LErrorDesc { 39 for _, p := range p.plugins { 40 if err := p.Request4C(pub, args, msg); err != nil { 41 return err 42 } 43 } 44 return nil 45 } 46 47 func (p *pluginManager) Send4C(pub *plugin.Context, msg *message.Message, err perror.LErrorDesc) perror.LErrorDesc { 48 for _, p := range p.plugins { 49 if err := p.Send4C(pub, msg, err); err != nil { 50 return err 51 } 52 } 53 return nil 54 } 55 56 func (p *pluginManager) AfterSend4C(pub *plugin.Context, msg *message.Message, err perror.LErrorDesc) perror.LErrorDesc { 57 for _, p := range p.plugins { 58 if err := p.AfterSend4C(pub, msg, err); err != nil { 59 return err 60 } 61 } 62 return nil 63 } 64 65 func (p *pluginManager) Receive4C(pub *plugin.Context, msg *message.Message, err perror.LErrorDesc) perror.LErrorDesc { 66 for _, p := range p.plugins { 67 if err := p.Receive4C(pub, msg, err); err != nil { 68 return err 69 } 70 } 71 return nil 72 } 73 74 func (p *pluginManager) AfterReceive4C(pub *plugin.Context, results []interface{}, err perror.LErrorDesc) perror.LErrorDesc { 75 for _, p := range p.plugins { 76 if err := p.AfterReceive4C(pub, results, err); err != nil { 77 return err 78 } 79 } 80 return nil 81 }