github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/server/plugin_manager.go (about)

     1  package server
     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  	"reflect"
     8  	"sync"
     9  )
    10  
    11  type pluginManager struct {
    12  	ctxPool sync.Pool
    13  	plugins []plugin.ServerPlugin
    14  }
    15  
    16  func newPluginManager(plugins []plugin.ServerPlugin) *pluginManager {
    17  	return &pluginManager{
    18  		ctxPool: sync.Pool{
    19  			New: func() interface{} {
    20  				return new(plugin.Context)
    21  			},
    22  		},
    23  		plugins: plugins,
    24  	}
    25  }
    26  
    27  func (m *pluginManager) AddPlugin(p plugin.ServerPlugin) {
    28  	m.plugins = append(m.plugins, p)
    29  }
    30  
    31  func (m *pluginManager) Size() int {
    32  	return len(m.plugins)
    33  }
    34  
    35  func (m *pluginManager) GetContext() *plugin.Context {
    36  	return m.ctxPool.Get().(*plugin.Context)
    37  }
    38  
    39  func (m *pluginManager) FreeContext(ctx *plugin.Context) {
    40  	m.ctxPool.Put(ctx)
    41  }
    42  
    43  func (m *pluginManager) Event4S(ev plugin.Event) (next bool) {
    44  	for _, p := range m.plugins {
    45  		if !p.Event4S(ev) {
    46  			return false
    47  		}
    48  	}
    49  	return true
    50  }
    51  
    52  func (m *pluginManager) Receive4S(pub *plugin.Context, msg *message.Message) perror.LErrorDesc {
    53  	for _, p := range m.plugins {
    54  		if err := p.Receive4S(pub, msg); err != nil {
    55  			return err
    56  		}
    57  	}
    58  	return nil
    59  }
    60  
    61  func (m *pluginManager) Call4S(pub *plugin.Context, args []reflect.Value, err perror.LErrorDesc) perror.LErrorDesc {
    62  	for _, p := range m.plugins {
    63  		if err := p.Call4S(pub, args, err); err != nil {
    64  			return err
    65  		}
    66  	}
    67  	return nil
    68  }
    69  
    70  func (m *pluginManager) AfterCall4S(pub *plugin.Context, args, results []reflect.Value, err perror.LErrorDesc) perror.LErrorDesc {
    71  	for _, p := range m.plugins {
    72  		if err := p.AfterCall4S(pub, args, results, err); err != nil {
    73  			return err
    74  		}
    75  	}
    76  	return nil
    77  }
    78  
    79  func (m *pluginManager) Send4S(pub *plugin.Context, msg *message.Message, err perror.LErrorDesc) perror.LErrorDesc {
    80  	for _, p := range m.plugins {
    81  		if err := p.Send4S(pub, msg, err); err != nil {
    82  			return err
    83  		}
    84  	}
    85  	return nil
    86  }
    87  
    88  func (m *pluginManager) AfterSend4S(pub *plugin.Context, msg *message.Message, err perror.LErrorDesc) perror.LErrorDesc {
    89  	for _, p := range m.plugins {
    90  		if err := p.AfterSend4S(pub, msg, err); err != nil {
    91  			return err
    92  		}
    93  	}
    94  	return nil
    95  }