gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/client/options.go (about) 1 package client 2 3 import ( 4 "context" 5 "time" 6 7 "gitee.com/liuxuezhan/go-micro-v1.18.0/broker" 8 "gitee.com/liuxuezhan/go-micro-v1.18.0/client/selector" 9 "gitee.com/liuxuezhan/go-micro-v1.18.0/codec" 10 "gitee.com/liuxuezhan/go-micro-v1.18.0/registry" 11 "gitee.com/liuxuezhan/go-micro-v1.18.0/transport" 12 ) 13 14 type Options struct { 15 // Used to select codec 16 ContentType string 17 18 // Plugged interfaces 19 Broker broker.Broker 20 Codecs map[string]codec.NewCodec 21 Registry registry.Registry 22 Selector selector.Selector 23 Transport transport.Transport 24 25 // Router sets the router 26 Router Router 27 28 // Connection Pool 29 PoolSize int 30 PoolTTL time.Duration 31 32 // Middleware for client 33 Wrappers []Wrapper 34 35 // Default Call Options 36 CallOptions CallOptions 37 38 // Other options for implementations of the interface 39 // can be stored in a context 40 Context context.Context 41 } 42 43 type CallOptions struct { 44 SelectOptions []selector.SelectOption 45 46 // Address of remote hosts 47 Address []string 48 // Backoff func 49 Backoff BackoffFunc 50 // Check if retriable func 51 Retry RetryFunc 52 // Transport Dial Timeout 53 DialTimeout time.Duration 54 // Number of Call attempts 55 Retries int 56 // Request/Response timeout 57 RequestTimeout time.Duration 58 59 // Middleware for low level call func 60 CallWrappers []CallWrapper 61 62 // Other options for implementations of the interface 63 // can be stored in a context 64 Context context.Context 65 } 66 67 type PublishOptions struct { 68 // Exchange is the routing exchange for the message 69 Exchange string 70 // Other options for implementations of the interface 71 // can be stored in a context 72 Context context.Context 73 } 74 75 type MessageOptions struct { 76 ContentType string 77 } 78 79 type RequestOptions struct { 80 ContentType string 81 Stream bool 82 83 // Other options for implementations of the interface 84 // can be stored in a context 85 Context context.Context 86 } 87 88 func newOptions(options ...Option) Options { 89 opts := Options{ 90 Codecs: make(map[string]codec.NewCodec), 91 CallOptions: CallOptions{ 92 Backoff: DefaultBackoff, 93 Retry: DefaultRetry, 94 Retries: DefaultRetries, 95 RequestTimeout: DefaultRequestTimeout, 96 DialTimeout: transport.DefaultDialTimeout, 97 }, 98 PoolSize: DefaultPoolSize, 99 PoolTTL: DefaultPoolTTL, 100 } 101 102 for _, o := range options { 103 o(&opts) 104 } 105 106 if len(opts.ContentType) == 0 { 107 opts.ContentType = DefaultContentType 108 } 109 110 if opts.Broker == nil { 111 opts.Broker = broker.DefaultBroker 112 } 113 114 if opts.Registry == nil { 115 opts.Registry = registry.DefaultRegistry 116 } 117 118 if opts.Selector == nil { 119 opts.Selector = selector.NewSelector( 120 selector.Registry(opts.Registry), 121 ) 122 } 123 124 if opts.Transport == nil { 125 opts.Transport = transport.DefaultTransport 126 } 127 128 if opts.Context == nil { 129 opts.Context = context.Background() 130 } 131 132 return opts 133 } 134 135 // Broker to be used for pub/sub 136 func Broker(b broker.Broker) Option { 137 return func(o *Options) { 138 o.Broker = b 139 } 140 } 141 142 // Codec to be used to encode/decode requests for a given content type 143 func Codec(contentType string, c codec.NewCodec) Option { 144 return func(o *Options) { 145 o.Codecs[contentType] = c 146 } 147 } 148 149 // Default content type of the client 150 func ContentType(ct string) Option { 151 return func(o *Options) { 152 o.ContentType = ct 153 } 154 } 155 156 // PoolSize sets the connection pool size 157 func PoolSize(d int) Option { 158 return func(o *Options) { 159 o.PoolSize = d 160 } 161 } 162 163 // PoolSize sets the connection pool size 164 func PoolTTL(d time.Duration) Option { 165 return func(o *Options) { 166 o.PoolTTL = d 167 } 168 } 169 170 // Registry to find nodes for a given service 171 func Registry(r registry.Registry) Option { 172 return func(o *Options) { 173 o.Registry = r 174 } 175 } 176 177 // Transport to use for communication e.g http, rabbitmq, etc 178 func Transport(t transport.Transport) Option { 179 return func(o *Options) { 180 o.Transport = t 181 } 182 } 183 184 // Select is used to select a node to route a request to 185 func Selector(s selector.Selector) Option { 186 return func(o *Options) { 187 o.Selector = s 188 } 189 } 190 191 // Adds a Wrapper to a list of options passed into the client 192 func Wrap(w Wrapper) Option { 193 return func(o *Options) { 194 o.Wrappers = append(o.Wrappers, w) 195 } 196 } 197 198 // Adds a Wrapper to the list of CallFunc wrappers 199 func WrapCall(cw ...CallWrapper) Option { 200 return func(o *Options) { 201 o.CallOptions.CallWrappers = append(o.CallOptions.CallWrappers, cw...) 202 } 203 } 204 205 // Backoff is used to set the backoff function used 206 // when retrying Calls 207 func Backoff(fn BackoffFunc) Option { 208 return func(o *Options) { 209 o.CallOptions.Backoff = fn 210 } 211 } 212 213 // Number of retries when making the request. 214 // Should this be a Call Option? 215 func Retries(i int) Option { 216 return func(o *Options) { 217 o.CallOptions.Retries = i 218 } 219 } 220 221 // Retry sets the retry function to be used when re-trying. 222 func Retry(fn RetryFunc) Option { 223 return func(o *Options) { 224 o.CallOptions.Retry = fn 225 } 226 } 227 228 // The request timeout. 229 // Should this be a Call Option? 230 func RequestTimeout(d time.Duration) Option { 231 return func(o *Options) { 232 o.CallOptions.RequestTimeout = d 233 } 234 } 235 236 // Transport dial timeout 237 func DialTimeout(d time.Duration) Option { 238 return func(o *Options) { 239 o.CallOptions.DialTimeout = d 240 } 241 } 242 243 // Call Options 244 245 // WithExchange sets the exchange to route a message through 246 func WithExchange(e string) PublishOption { 247 return func(o *PublishOptions) { 248 o.Exchange = e 249 } 250 } 251 252 // WithAddress sets the remote addresses to use rather than using service discovery 253 func WithAddress(a ...string) CallOption { 254 return func(o *CallOptions) { 255 o.Address = a 256 } 257 } 258 259 func WithSelectOption(so ...selector.SelectOption) CallOption { 260 return func(o *CallOptions) { 261 o.SelectOptions = append(o.SelectOptions, so...) 262 } 263 } 264 265 // WithCallWrapper is a CallOption which adds to the existing CallFunc wrappers 266 func WithCallWrapper(cw ...CallWrapper) CallOption { 267 return func(o *CallOptions) { 268 o.CallWrappers = append(o.CallWrappers, cw...) 269 } 270 } 271 272 // WithBackoff is a CallOption which overrides that which 273 // set in Options.CallOptions 274 func WithBackoff(fn BackoffFunc) CallOption { 275 return func(o *CallOptions) { 276 o.Backoff = fn 277 } 278 } 279 280 // WithRetry is a CallOption which overrides that which 281 // set in Options.CallOptions 282 func WithRetry(fn RetryFunc) CallOption { 283 return func(o *CallOptions) { 284 o.Retry = fn 285 } 286 } 287 288 // WithRetries is a CallOption which overrides that which 289 // set in Options.CallOptions 290 func WithRetries(i int) CallOption { 291 return func(o *CallOptions) { 292 o.Retries = i 293 } 294 } 295 296 // WithRequestTimeout is a CallOption which overrides that which 297 // set in Options.CallOptions 298 func WithRequestTimeout(d time.Duration) CallOption { 299 return func(o *CallOptions) { 300 o.RequestTimeout = d 301 } 302 } 303 304 // WithDialTimeout is a CallOption which overrides that which 305 // set in Options.CallOptions 306 func WithDialTimeout(d time.Duration) CallOption { 307 return func(o *CallOptions) { 308 o.DialTimeout = d 309 } 310 } 311 312 // Request Options 313 314 func WithContentType(ct string) RequestOption { 315 return func(o *RequestOptions) { 316 o.ContentType = ct 317 } 318 } 319 320 func StreamingRequest() RequestOption { 321 return func(o *RequestOptions) { 322 o.Stream = true 323 } 324 } 325 326 // WithRouter sets the client router 327 func WithRouter(r Router) Option { 328 return func(o *Options) { 329 o.Router = r 330 } 331 }