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