go-micro.dev/v5@v5.12.0/client/options.go (about)

     1  package client
     2  
     3  import (
     4  	"context"
     5  	"time"
     6  
     7  	"go-micro.dev/v5/broker"
     8  	"go-micro.dev/v5/codec"
     9  	"go-micro.dev/v5/logger"
    10  	"go-micro.dev/v5/registry"
    11  	"go-micro.dev/v5/selector"
    12  	"go-micro.dev/v5/transport"
    13  )
    14  
    15  var (
    16  	// DefaultBackoff is the default backoff function for retries.
    17  	DefaultBackoff = exponentialBackoff
    18  	// DefaultRetry is the default check-for-retry function for retries.
    19  	DefaultRetry = RetryOnError
    20  	// DefaultRetries is the default number of times a request is tried.
    21  	DefaultRetries = 5
    22  	// DefaultRequestTimeout is the default request timeout.
    23  	DefaultRequestTimeout = time.Second * 30
    24  	// DefaultConnectionTimeout is the default connection timeout.
    25  	DefaultConnectionTimeout = time.Second * 5
    26  	// DefaultPoolSize sets the connection pool size.
    27  	DefaultPoolSize = 100
    28  	// DefaultPoolTTL sets the connection pool ttl.
    29  	DefaultPoolTTL = time.Minute
    30  	// DefaultPoolCloseTimeout sets the connection pool colse timeout.
    31  	DefaultPoolCloseTimeout = time.Second
    32  )
    33  
    34  // Options are the Client options.
    35  type Options struct {
    36  
    37  	// Default Call Options
    38  	CallOptions CallOptions
    39  
    40  	// Router sets the router
    41  	Router Router
    42  
    43  	Registry  registry.Registry
    44  	Selector  selector.Selector
    45  	Transport transport.Transport
    46  
    47  	// Plugged interfaces
    48  	Broker broker.Broker
    49  
    50  	// Logger is the underline logger
    51  	Logger logger.Logger
    52  
    53  	// Other options for implementations of the interface
    54  	// can be stored in a context
    55  	Context context.Context
    56  	Codecs  map[string]codec.NewCodec
    57  
    58  	// Response cache
    59  	Cache *Cache
    60  
    61  	// Used to select codec
    62  	ContentType string
    63  
    64  	// Middleware for client
    65  	Wrappers []Wrapper
    66  
    67  	// Connection Pool
    68  	PoolSize         int
    69  	PoolTTL          time.Duration
    70  	PoolCloseTimeout time.Duration
    71  }
    72  
    73  // CallOptions are options used to make calls to a server.
    74  type CallOptions struct {
    75  
    76  	// Other options for implementations of the interface
    77  	// can be stored in a context
    78  	Context context.Context
    79  	// Backoff func
    80  	Backoff BackoffFunc
    81  	// Check if retriable func
    82  	Retry         RetryFunc
    83  	SelectOptions []selector.SelectOption
    84  
    85  	// Address of remote hosts
    86  	Address []string
    87  
    88  	// Middleware for low level call func
    89  	CallWrappers []CallWrapper
    90  
    91  	// ConnectionTimeout of one request to the server.
    92  	// Set this lower than the RequestTimeout to enable retries on connection timeout.
    93  	ConnectionTimeout time.Duration
    94  	// Request/Response timeout of entire srv.Call, for single request timeout set ConnectionTimeout.
    95  	RequestTimeout time.Duration
    96  	// Stream timeout for the stream
    97  	StreamTimeout time.Duration
    98  	// Duration to cache the response for
    99  	CacheExpiry time.Duration
   100  	// Transport Dial Timeout. Used for initial dial to establish a connection.
   101  	DialTimeout time.Duration
   102  	// Number of Call attempts
   103  	Retries int
   104  	// Use the services own auth token
   105  	ServiceToken bool
   106  	// ConnClose sets the Connection: close header.
   107  	ConnClose bool
   108  }
   109  
   110  type PublishOptions struct {
   111  	// Other options for implementations of the interface
   112  	// can be stored in a context
   113  	Context context.Context
   114  	// Exchange is the routing exchange for the message
   115  	Exchange string
   116  }
   117  
   118  type MessageOptions struct {
   119  	ContentType string
   120  }
   121  
   122  type RequestOptions struct {
   123  
   124  	// Other options for implementations of the interface
   125  	// can be stored in a context
   126  	Context     context.Context
   127  	ContentType string
   128  	Stream      bool
   129  }
   130  
   131  // NewOptions creates new Client options.
   132  func NewOptions(options ...Option) Options {
   133  	opts := Options{
   134  		Cache:       NewCache(),
   135  		Context:     context.Background(),
   136  		ContentType: DefaultContentType,
   137  		Codecs:      make(map[string]codec.NewCodec),
   138  		CallOptions: CallOptions{
   139  			Backoff:           DefaultBackoff,
   140  			Retry:             DefaultRetry,
   141  			Retries:           DefaultRetries,
   142  			RequestTimeout:    DefaultRequestTimeout,
   143  			ConnectionTimeout: DefaultConnectionTimeout,
   144  			DialTimeout:       transport.DefaultDialTimeout,
   145  		},
   146  		PoolSize:         DefaultPoolSize,
   147  		PoolTTL:          DefaultPoolTTL,
   148  		PoolCloseTimeout: DefaultPoolCloseTimeout,
   149  		Broker:           broker.DefaultBroker,
   150  		Selector:         selector.DefaultSelector,
   151  		Registry:         registry.DefaultRegistry,
   152  		Transport:        transport.DefaultTransport,
   153  		Logger:           logger.DefaultLogger,
   154  	}
   155  
   156  	for _, o := range options {
   157  		o(&opts)
   158  	}
   159  
   160  	return opts
   161  }
   162  
   163  // Broker to be used for pub/sub.
   164  func Broker(b broker.Broker) Option {
   165  	return func(o *Options) {
   166  		o.Broker = b
   167  	}
   168  }
   169  
   170  // Codec to be used to encode/decode requests for a given content type.
   171  func Codec(contentType string, c codec.NewCodec) Option {
   172  	return func(o *Options) {
   173  		o.Codecs[contentType] = c
   174  	}
   175  }
   176  
   177  // ContentType sets the default content type of the client.
   178  func ContentType(ct string) Option {
   179  	return func(o *Options) {
   180  		o.ContentType = ct
   181  	}
   182  }
   183  
   184  // PoolSize sets the connection pool size.
   185  func PoolSize(d int) Option {
   186  	return func(o *Options) {
   187  		o.PoolSize = d
   188  	}
   189  }
   190  
   191  // PoolTTL sets the connection pool ttl.
   192  func PoolTTL(d time.Duration) Option {
   193  	return func(o *Options) {
   194  		o.PoolTTL = d
   195  	}
   196  }
   197  
   198  // PoolCloseTimeout sets the connection pool close timeout.
   199  func PoolCloseTimeout(d time.Duration) Option {
   200  	return func(o *Options) {
   201  		o.PoolCloseTimeout = d
   202  	}
   203  }
   204  
   205  // Registry to find nodes for a given service.
   206  func Registry(r registry.Registry) Option {
   207  	return func(o *Options) {
   208  		o.Registry = r
   209  		// set in the selector
   210  		o.Selector.Init(selector.Registry(r))
   211  	}
   212  }
   213  
   214  // Transport to use for communication e.g http, rabbitmq, etc.
   215  func Transport(t transport.Transport) Option {
   216  	return func(o *Options) {
   217  		o.Transport = t
   218  	}
   219  }
   220  
   221  // Select is used to select a node to route a request to.
   222  func Selector(s selector.Selector) Option {
   223  	return func(o *Options) {
   224  		o.Selector = s
   225  	}
   226  }
   227  
   228  // Adds a Wrapper to a list of options passed into the client.
   229  func Wrap(w Wrapper) Option {
   230  	return func(o *Options) {
   231  		o.Wrappers = append(o.Wrappers, w)
   232  	}
   233  }
   234  
   235  // Adds a Wrapper to the list of CallFunc wrappers.
   236  func WrapCall(cw ...CallWrapper) Option {
   237  	return func(o *Options) {
   238  		o.CallOptions.CallWrappers = append(o.CallOptions.CallWrappers, cw...)
   239  	}
   240  }
   241  
   242  // Backoff is used to set the backoff function used
   243  // when retrying Calls.
   244  func Backoff(fn BackoffFunc) Option {
   245  	return func(o *Options) {
   246  		o.CallOptions.Backoff = fn
   247  	}
   248  }
   249  
   250  // Retries set the number of retries when making the request.
   251  func Retries(i int) Option {
   252  	return func(o *Options) {
   253  		o.CallOptions.Retries = i
   254  	}
   255  }
   256  
   257  // Retry sets the retry function to be used when re-trying.
   258  func Retry(fn RetryFunc) Option {
   259  	return func(o *Options) {
   260  		o.CallOptions.Retry = fn
   261  	}
   262  }
   263  
   264  // ConnectionTimeout sets the connection timeout
   265  func ConnectionTimeout(t time.Duration) Option {
   266  	return func(o *Options) {
   267  		o.CallOptions.ConnectionTimeout = t
   268  	}
   269  }
   270  
   271  // RequestTimeout set the request timeout.
   272  func RequestTimeout(d time.Duration) Option {
   273  	return func(o *Options) {
   274  		o.CallOptions.RequestTimeout = d
   275  	}
   276  }
   277  
   278  // StreamTimeout sets the stream timeout.
   279  func StreamTimeout(d time.Duration) Option {
   280  	return func(o *Options) {
   281  		o.CallOptions.StreamTimeout = d
   282  	}
   283  }
   284  
   285  // DialTimeout sets the transport dial timeout.
   286  func DialTimeout(d time.Duration) Option {
   287  	return func(o *Options) {
   288  		o.CallOptions.DialTimeout = d
   289  	}
   290  }
   291  
   292  // Call Options
   293  
   294  // WithExchange sets the exchange to route a message through.
   295  func WithExchange(e string) PublishOption {
   296  	return func(o *PublishOptions) {
   297  		o.Exchange = e
   298  	}
   299  }
   300  
   301  // PublishContext sets the context in publish options.
   302  func PublishContext(ctx context.Context) PublishOption {
   303  	return func(o *PublishOptions) {
   304  		o.Context = ctx
   305  	}
   306  }
   307  
   308  // WithAddress sets the remote addresses to use rather than using service discovery.
   309  func WithAddress(a ...string) CallOption {
   310  	return func(o *CallOptions) {
   311  		o.Address = a
   312  	}
   313  }
   314  
   315  func WithSelectOption(so ...selector.SelectOption) CallOption {
   316  	return func(o *CallOptions) {
   317  		o.SelectOptions = append(o.SelectOptions, so...)
   318  	}
   319  }
   320  
   321  // WithCallWrapper is a CallOption which adds to the existing CallFunc wrappers.
   322  func WithCallWrapper(cw ...CallWrapper) CallOption {
   323  	return func(o *CallOptions) {
   324  		o.CallWrappers = append(o.CallWrappers, cw...)
   325  	}
   326  }
   327  
   328  // WithBackoff is a CallOption which overrides that which
   329  // set in Options.CallOptions.
   330  func WithBackoff(fn BackoffFunc) CallOption {
   331  	return func(o *CallOptions) {
   332  		o.Backoff = fn
   333  	}
   334  }
   335  
   336  // WithRetry is a CallOption which overrides that which
   337  // set in Options.CallOptions.
   338  func WithRetry(fn RetryFunc) CallOption {
   339  	return func(o *CallOptions) {
   340  		o.Retry = fn
   341  	}
   342  }
   343  
   344  // WithRetries sets the number of tries for a call.
   345  // This CallOption overrides Options.CallOptions.
   346  func WithRetries(i int) CallOption {
   347  	return func(o *CallOptions) {
   348  		o.Retries = i
   349  	}
   350  }
   351  
   352  // WithRequestTimeout is a CallOption which overrides that which
   353  // set in Options.CallOptions.
   354  func WithRequestTimeout(d time.Duration) CallOption {
   355  	return func(o *CallOptions) {
   356  		o.RequestTimeout = d
   357  	}
   358  }
   359  
   360  // WithConnClose sets the Connection header to close.
   361  func WithConnClose() CallOption {
   362  	return func(o *CallOptions) {
   363  		o.ConnClose = true
   364  	}
   365  }
   366  
   367  // WithStreamTimeout sets the stream timeout.
   368  func WithStreamTimeout(d time.Duration) CallOption {
   369  	return func(o *CallOptions) {
   370  		o.StreamTimeout = d
   371  	}
   372  }
   373  
   374  // WithDialTimeout is a CallOption which overrides that which
   375  // set in Options.CallOptions.
   376  func WithDialTimeout(d time.Duration) CallOption {
   377  	return func(o *CallOptions) {
   378  		o.DialTimeout = d
   379  	}
   380  }
   381  
   382  // WithServiceToken is a CallOption which overrides the
   383  // authorization header with the services own auth token.
   384  func WithServiceToken() CallOption {
   385  	return func(o *CallOptions) {
   386  		o.ServiceToken = true
   387  	}
   388  }
   389  
   390  // WithCache is a CallOption which sets the duration the response
   391  // shoull be cached for.
   392  func WithCache(c time.Duration) CallOption {
   393  	return func(o *CallOptions) {
   394  		o.CacheExpiry = c
   395  	}
   396  }
   397  
   398  func WithMessageContentType(ct string) MessageOption {
   399  	return func(o *MessageOptions) {
   400  		o.ContentType = ct
   401  	}
   402  }
   403  
   404  // Request Options
   405  
   406  func WithContentType(ct string) RequestOption {
   407  	return func(o *RequestOptions) {
   408  		o.ContentType = ct
   409  	}
   410  }
   411  
   412  func StreamingRequest() RequestOption {
   413  	return func(o *RequestOptions) {
   414  		o.Stream = true
   415  	}
   416  }
   417  
   418  // WithRouter sets the client router.
   419  func WithRouter(r Router) Option {
   420  	return func(o *Options) {
   421  		o.Router = r
   422  	}
   423  }
   424  
   425  // WithLogger sets the underline logger.
   426  func WithLogger(l logger.Logger) Option {
   427  	return func(o *Options) {
   428  		o.Logger = l
   429  	}
   430  }