github.com/nyan233/littlerpc@v0.4.6-0.20230316182519-0c8d5c48abaf/core/client/options.go (about) 1 package client 2 3 import ( 4 "crypto/tls" 5 "github.com/nyan233/littlerpc/core/client/loadbalance" 6 "github.com/nyan233/littlerpc/core/common/errorhandler" 7 "github.com/nyan233/littlerpc/core/common/logger" 8 "github.com/nyan233/littlerpc/core/common/msgparser" 9 msgwriter2 "github.com/nyan233/littlerpc/core/common/msgwriter" 10 "github.com/nyan233/littlerpc/core/middle/codec" 11 "github.com/nyan233/littlerpc/core/middle/packer" 12 "github.com/nyan233/littlerpc/core/middle/plugin" 13 perror "github.com/nyan233/littlerpc/core/protocol/error" 14 "github.com/nyan233/littlerpc/core/protocol/message" 15 "time" 16 ) 17 18 type Option func(config *Config) 19 20 func (opt Option) apply(config *Config) { 21 opt(config) 22 } 23 24 // DirectConfig 这个接口不保证兼容性, 应该谨慎使用 25 // Config中的内容可能会变动, 或者被修改了语义 26 func DirectConfig(uCfg Config) Option { 27 return func(config *Config) { 28 *config = uCfg 29 } 30 } 31 32 func WithDefault() Option { 33 return func(config *Config) { 34 WithCustomLogger(logger.DefaultLogger)(config) 35 WithPacker(message.DefaultPacker)(config) 36 WithCodec(message.DefaultCodec)(config) 37 WithNetWork("nbio_tcp")(config) 38 WithMuxConnectionNumber(8)(config) 39 WithNoStackTrace()(config) 40 WithPoolSize(0)(config) 41 WithNoMuxWriter()(config) 42 WithTraitMessageParser()(config) 43 WithBalancerFactory(loadbalance.New)(config) 44 WithResolverUpdateInterval(time.Second * 120)(config) 45 } 46 } 47 48 func WithJsonRpc2() Option { 49 return func(config *Config) { 50 WithJsonRpc2Writer()(config) 51 WithCodec(message.DefaultCodec) 52 WithPacker(message.DefaultPacker) 53 } 54 } 55 56 func WithNetWork(network string) Option { 57 return func(config *Config) { 58 config.NetWork = network 59 } 60 } 61 62 func WithDefaultKeepAlive() Option { 63 return func(config *Config) { 64 config.KeepAlive = true 65 config.KeepAliveTimeout = time.Second * 120 66 } 67 } 68 69 func WithKeepAlive(timeOut time.Duration) Option { 70 return func(config *Config) { 71 config.KeepAlive = false 72 config.KeepAliveTimeout = timeOut 73 } 74 } 75 76 func WithResolver(r loadbalance.ResolverFunc) Option { 77 return func(config *Config) { 78 config.BalancerResolverFunc = r 79 } 80 } 81 82 func WithHttpResolver(url string) Option { 83 return WithResolver(loadbalance.DefaultHttpResolver(url)) 84 } 85 86 func WithLiveResolver(splitAddr string) Option { 87 return WithResolver(loadbalance.DefaultLiveResolver(splitAddr)) 88 } 89 90 func WithFileResolver(path string) Option { 91 return WithResolver(loadbalance.DefaultFileResolver(path)) 92 } 93 94 func WithOpenLoadBalance() Option { 95 return func(config *Config) { 96 config.OpenLoadBalance = true 97 } 98 } 99 100 func WithBalancerScheme(scheme string) Option { 101 return func(config *Config) { 102 config.BalancerScheme = scheme 103 } 104 } 105 106 func WithBalancerTailConfig(config interface{}) Option { 107 return func(config *Config) { 108 config.BalancerTailConfig = config 109 } 110 } 111 112 func WithResolverUpdateInterval(updateInterval time.Duration) Option { 113 return func(config *Config) { 114 config.ResolverUpdateInterval = updateInterval 115 } 116 } 117 118 func WithBalancerFactory(fn func(loadbalance.Config) loadbalance.Balancer) Option { 119 return func(config *Config) { 120 config.BalancerFactory = fn 121 } 122 } 123 124 func WithTlsClient(tlsC *tls.Config) Option { 125 return func(config *Config) { 126 config.TlsConfig = tlsC 127 } 128 } 129 130 func WithAddress(addr string) Option { 131 return func(config *Config) { 132 config.ServerAddr = addr 133 } 134 } 135 136 func WithCustomLogger(logger logger.LLogger) Option { 137 return func(config *Config) { 138 config.Logger = logger 139 } 140 } 141 142 func WithPacker(scheme string) Option { 143 return func(config *Config) { 144 config.Packer = packer.Get(scheme) 145 } 146 } 147 148 func WithCodec(scheme string) Option { 149 return func(config *Config) { 150 config.Codec = codec.Get(scheme) 151 } 152 } 153 154 func WithMuxConnection(ok bool) Option { 155 return func(config *Config) { 156 if !ok { 157 config.MuxConnection = 1 158 } 159 } 160 } 161 162 func WithMuxConnectionNumber(n int) Option { 163 return func(config *Config) { 164 config.MuxConnection = n 165 } 166 } 167 168 func WithProtocol(scheme string) Option { 169 return func(config *Config) { 170 config.NetWork = scheme 171 } 172 } 173 174 func WithPoolSize(size int) Option { 175 return func(config *Config) { 176 if size == 0 { 177 config.PoolSize = int32(size) 178 } 179 } 180 } 181 182 func WithPlugin(plugin plugin.ClientPlugin) Option { 183 return func(config *Config) { 184 config.Plugins = append(config.Plugins, plugin) 185 } 186 } 187 188 func WithStackTrace() Option { 189 return WithErrHandler(errorhandler.NewStackTrace()) 190 } 191 192 func WithNoStackTrace() Option { 193 return WithErrHandler(errorhandler.DefaultErrHandler) 194 } 195 196 func WithErrHandler(eh perror.LErrors) Option { 197 return func(config *Config) { 198 config.ErrHandler = eh 199 } 200 } 201 202 func WithTraitMessageParser() Option { 203 return WithMessageParser(msgparser.DefaultParser) 204 } 205 206 func WithMessageParser(scheme string) Option { 207 return func(config *Config) { 208 config.ParserFactory = msgparser.Get(scheme) 209 } 210 } 211 212 func WithMessageWriter(writer msgwriter2.Writer) Option { 213 return func(config *Config) { 214 config.Writer = writer 215 } 216 } 217 218 func WithNoMuxWriter() Option { 219 return WithMessageWriter(msgwriter2.NewLRPCNoMux()) 220 } 221 222 func WithMuxWriter() Option { 223 return WithMessageWriter(msgwriter2.NewLRPCMux()) 224 } 225 226 func WithJsonRpc2Writer() Option { 227 return WithMessageWriter(msgwriter2.NewJsonRPC2()) 228 } 229 230 func WithHashLoadBalance() Option { 231 return WithBalancerScheme("hash") 232 } 233 234 func WithRoundRobinBalance() Option { 235 return WithBalancerScheme("roundRobin") 236 } 237 238 func WithRandomBalance() Option { 239 return WithBalancerScheme("random") 240 } 241 242 func WithConsistentHashBalance() Option { 243 return WithBalancerScheme("consistentHash") 244 } 245 246 func WithMessageParserOnRead() Option { 247 return func(config *Config) { 248 config.RegisterMPOnRead = true 249 } 250 }