github.com/annwntech/go-micro/v2@v2.9.5/client/options.go (about)

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