github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/server/options.go (about) 1 package server 2 3 import ( 4 "github.com/lesismal/llib/std/crypto/tls" 5 "github.com/nyan233/littlerpc/core/common/errorhandler" 6 logger2 "github.com/nyan233/littlerpc/core/common/logger" 7 "github.com/nyan233/littlerpc/core/common/metadata" 8 "github.com/nyan233/littlerpc/core/common/msgparser" 9 "github.com/nyan233/littlerpc/core/common/msgwriter" 10 "github.com/nyan233/littlerpc/core/middle/plugin" 11 perror "github.com/nyan233/littlerpc/core/protocol/error" 12 "github.com/nyan233/littlerpc/internal/pool" 13 "runtime" 14 "time" 15 ) 16 17 type Option func(config *Config) 18 19 func (opt Option) apply(config *Config) { 20 opt(config) 21 } 22 23 func DirectConfig(uCfg Config) Option { 24 return func(config *Config) { 25 *config = uCfg 26 } 27 } 28 29 func WithLogger(logger logger2.LLogger) Option { 30 return func(config *Config) { 31 config.Logger = logger 32 } 33 } 34 35 func WithDefaultServer() Option { 36 return func(config *Config) { 37 WithLogger(logger2.DefaultLogger)(config) 38 WithKeepAlive(false, time.Second*120)(config) 39 WithNetwork("nbio_tcp")(config) 40 WithNoStackTrace()(config) 41 WithExecPoolArgument(int32(runtime.NumCPU()*8), pool.MaxTaskPoolSize, 2048)(config) 42 WithTraitMessageParser()(config) 43 WithTraitMessageWriter()(config) 44 WithTaskPool()(config) 45 WithDefaultProcessOption(metadata.ProcessOption{})(config) 46 WithReadBufferSize(64 * (1 << 10))(config) // 64KB 47 WithWriteBufferSize(1 * (1 << 20))(config) // 1MB 48 } 49 } 50 51 func WithAddressServer(adds ...string) Option { 52 return func(config *Config) { 53 config.Address = append(config.Address, adds...) 54 } 55 } 56 57 func WithTlsServer(tlsC *tls.Config) Option { 58 return func(config *Config) { 59 config.TlsConfig = tlsC 60 } 61 } 62 63 func WithNetwork(scheme string) Option { 64 return func(config *Config) { 65 config.NetWork = scheme 66 } 67 } 68 69 func WithOpenLogger(ok bool) Option { 70 return func(config *Config) { 71 if !ok { 72 config.Logger = logger2.NilLogger{} 73 } 74 } 75 } 76 77 func WithPlugin(plg plugin.ServerPlugin) Option { 78 return func(config *Config) { 79 config.Plugins = append(config.Plugins, plg) 80 } 81 } 82 83 func WithStackTrace() Option { 84 return WithErrHandler(errorhandler.NewStackTrace()) 85 } 86 87 func WithNoStackTrace() Option { 88 return WithErrHandler(errorhandler.DefaultErrHandler) 89 } 90 91 func WithErrHandler(eh perror.LErrors) Option { 92 return func(config *Config) { 93 config.ErrHandler = eh 94 } 95 } 96 97 func WithExecPool(builder pool.TaskPoolBuilder[string]) Option { 98 return func(config *Config) { 99 config.ExecPoolBuilder = builder 100 } 101 } 102 103 func WithFixedPool() Option { 104 return WithExecPool(poolBuilder[string](pool.NewFixedPool[string])) 105 } 106 107 func WithTaskPool() Option { 108 return WithExecPool(poolBuilder[string](pool.NewTaskPool[string])) 109 } 110 111 func WithExecPoolArgument(minSize, maxSize, bufSize int32) Option { 112 return func(config *Config) { 113 config.PoolMinSize = minSize 114 config.PoolMaxSize = maxSize 115 config.PoolBufferSize = bufSize 116 } 117 } 118 119 func WithDebug(debug bool) Option { 120 return func(config *Config) { 121 config.Debug = debug 122 } 123 } 124 125 func WithMessageParser(scheme string) Option { 126 return func(config *Config) { 127 config.ParserFactory = msgparser.Get(scheme) 128 } 129 } 130 131 func WithTraitMessageParser() Option { 132 return WithMessageParser(msgparser.DefaultParser) 133 } 134 135 func WithMessageWriter(scheme string) Option { 136 return func(config *Config) { 137 config.WriterFactory = msgwriter.Get(scheme) 138 } 139 } 140 141 func WithTraitMessageWriter() Option { 142 return WithMessageWriter(msgwriter.DefaultWriter) 143 } 144 145 func WithKeepAlive(open bool, timeOut time.Duration) Option { 146 return func(config *Config) { 147 config.KeepAlive = open 148 config.KeepAliveTimeout = timeOut 149 } 150 } 151 152 func WithReflection() Option { 153 return func(config *Config) { 154 config.OpenReflection = true 155 } 156 } 157 158 func WithDefaultProcessOption(opt metadata.ProcessOption) Option { 159 return func(config *Config) { 160 config.DefaultProcessOption = opt 161 } 162 } 163 164 func WithReadBufferSize(size int) Option { 165 return func(config *Config) { 166 config.ReadBufferSize = size 167 } 168 } 169 170 func WithWriteBufferSize(size int) Option { 171 return func(config *Config) { 172 config.WriteBufferSize = size 173 } 174 } 175 176 func WithMessageParserOnRead() Option { 177 return func(config *Config) { 178 config.RegisterMPOnRead = true 179 } 180 }