github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/client/options.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/client/options.go
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/tickoalcantara12/micro/v3/service/broker"
    24  	"github.com/tickoalcantara12/micro/v3/service/broker/memory"
    25  	"github.com/tickoalcantara12/micro/v3/service/network/transport"
    26  	thttp "github.com/tickoalcantara12/micro/v3/service/network/transport/http"
    27  	"github.com/tickoalcantara12/micro/v3/service/registry"
    28  	"github.com/tickoalcantara12/micro/v3/service/router"
    29  	regRouter "github.com/tickoalcantara12/micro/v3/service/router/registry"
    30  	"github.com/tickoalcantara12/micro/v3/util/codec"
    31  	"github.com/tickoalcantara12/micro/v3/util/selector"
    32  	"github.com/tickoalcantara12/micro/v3/util/selector/roundrobin"
    33  )
    34  
    35  type Options struct {
    36  	// Used to select codec
    37  	ContentType string
    38  	// Proxy address to send requests via
    39  	Proxy string
    40  
    41  	// Plugged interfaces
    42  	Broker    broker.Broker
    43  	Codecs    map[string]codec.NewCodec
    44  	Router    router.Router
    45  	Selector  selector.Selector
    46  	Transport transport.Transport
    47  
    48  	// Lookup used for looking up routes
    49  	Lookup LookupFunc
    50  
    51  	// Connection Pool
    52  	PoolSize int
    53  	PoolTTL  time.Duration
    54  
    55  	// Middleware for client
    56  	Wrappers []Wrapper
    57  
    58  	// Default Call Options
    59  	CallOptions CallOptions
    60  
    61  	// Other options for implementations of the interface
    62  	// can be stored in a context
    63  	Context context.Context
    64  }
    65  
    66  type CallOptions struct {
    67  	// Address of remote hosts
    68  	Address []string
    69  	// Backoff func
    70  	Backoff BackoffFunc
    71  	// Transport Dial Timeout
    72  	DialTimeout time.Duration
    73  	// Number of Call attempts
    74  	Retries int
    75  	// Check if retriable func
    76  	Retry RetryFunc
    77  	// Request/Response timeout
    78  	RequestTimeout time.Duration
    79  	// Router to use for this call
    80  	Router router.Router
    81  	// Selector to use for the call
    82  	Selector selector.Selector
    83  	// SelectOptions to use when selecting a route
    84  	SelectOptions []selector.SelectOption
    85  	// Stream timeout for the stream
    86  	StreamTimeout time.Duration
    87  	// Use the auth token as the authorization header
    88  	AuthToken bool
    89  	// Network to lookup the route within
    90  	Network string
    91  
    92  	// Middleware for low level call func
    93  	CallWrappers []CallWrapper
    94  
    95  	// Other options for implementations of the interface
    96  	// can be stored in a context
    97  	Context context.Context
    98  }
    99  
   100  type PublishOptions struct {
   101  	// Exchange is the routing exchange for the message
   102  	Exchange string
   103  	// Other options for implementations of the interface
   104  	// can be stored in a context
   105  	Context context.Context
   106  }
   107  
   108  type MessageOptions struct {
   109  	ContentType string
   110  }
   111  
   112  type RequestOptions struct {
   113  	ContentType string
   114  	Stream      bool
   115  
   116  	// Other options for implementations of the interface
   117  	// can be stored in a context
   118  	Context context.Context
   119  }
   120  
   121  func NewOptions(options ...Option) Options {
   122  	opts := Options{
   123  		Context:     context.Background(),
   124  		ContentType: "application/protobuf",
   125  		Codecs:      make(map[string]codec.NewCodec),
   126  		CallOptions: CallOptions{
   127  			Backoff:        DefaultBackoff,
   128  			Retry:          DefaultRetry,
   129  			Retries:        DefaultRetries,
   130  			RequestTimeout: DefaultRequestTimeout,
   131  			DialTimeout:    transport.DefaultDialTimeout,
   132  		},
   133  		Lookup:    LookupRoute,
   134  		PoolSize:  DefaultPoolSize,
   135  		PoolTTL:   DefaultPoolTTL,
   136  		Broker:    memory.NewBroker(),
   137  		Router:    regRouter.NewRouter(),
   138  		Selector:  roundrobin.NewSelector(),
   139  		Transport: thttp.NewTransport(),
   140  	}
   141  
   142  	for _, o := range options {
   143  		o(&opts)
   144  	}
   145  
   146  	return opts
   147  }
   148  
   149  // Broker to be used for pub/sub
   150  func Broker(b broker.Broker) Option {
   151  	return func(o *Options) {
   152  		o.Broker = b
   153  	}
   154  }
   155  
   156  // Codec to be used to encode/decode requests for a given content type
   157  func Codec(contentType string, c codec.NewCodec) Option {
   158  	return func(o *Options) {
   159  		o.Codecs[contentType] = c
   160  	}
   161  }
   162  
   163  // Default content type of the client
   164  func ContentType(ct string) Option {
   165  	return func(o *Options) {
   166  		o.ContentType = ct
   167  	}
   168  }
   169  
   170  // Proxy sets the proxy address
   171  func Proxy(addr string) Option {
   172  	return func(o *Options) {
   173  		o.Proxy = addr
   174  	}
   175  }
   176  
   177  // PoolSize sets the connection pool size
   178  func PoolSize(d int) Option {
   179  	return func(o *Options) {
   180  		o.PoolSize = d
   181  	}
   182  }
   183  
   184  // PoolTTL sets the connection pool ttl
   185  func PoolTTL(d time.Duration) Option {
   186  	return func(o *Options) {
   187  		o.PoolTTL = d
   188  	}
   189  }
   190  
   191  // Transport to use for communication e.g http, rabbitmq, etc
   192  func Transport(t transport.Transport) Option {
   193  	return func(o *Options) {
   194  		o.Transport = t
   195  	}
   196  }
   197  
   198  // Registry sets the routers registry
   199  func Registry(r registry.Registry) Option {
   200  	return func(o *Options) {
   201  		o.Router.Init(router.Registry(r))
   202  	}
   203  }
   204  
   205  // Router is used to lookup routes for a service
   206  func Router(r router.Router) Option {
   207  	return func(o *Options) {
   208  		o.Router = r
   209  	}
   210  }
   211  
   212  // Selector is used to select a route
   213  func Selector(s selector.Selector) Option {
   214  	return func(o *Options) {
   215  		o.Selector = s
   216  	}
   217  }
   218  
   219  // Adds a Wrapper to a list of options passed into the client
   220  func Wrap(w Wrapper) Option {
   221  	return func(o *Options) {
   222  		o.Wrappers = append(o.Wrappers, w)
   223  	}
   224  }
   225  
   226  // Adds a Wrapper to the list of CallFunc wrappers
   227  func WrapCall(cw ...CallWrapper) Option {
   228  	return func(o *Options) {
   229  		o.CallOptions.CallWrappers = append(o.CallOptions.CallWrappers, cw...)
   230  	}
   231  }
   232  
   233  // Backoff is used to set the backoff function used
   234  // when retrying Calls
   235  func Backoff(fn BackoffFunc) Option {
   236  	return func(o *Options) {
   237  		o.CallOptions.Backoff = fn
   238  	}
   239  }
   240  
   241  // Lookup sets the lookup function to use for resolving service names
   242  func Lookup(l LookupFunc) Option {
   243  	return func(o *Options) {
   244  		o.Lookup = l
   245  	}
   246  }
   247  
   248  // Number of retries when making the request.
   249  // Should this be a Call Option?
   250  func Retries(i int) Option {
   251  	return func(o *Options) {
   252  		o.CallOptions.Retries = i
   253  	}
   254  }
   255  
   256  // Retry sets the retry function to be used when re-trying.
   257  func Retry(fn RetryFunc) Option {
   258  	return func(o *Options) {
   259  		o.CallOptions.Retry = fn
   260  	}
   261  }
   262  
   263  // The request timeout.
   264  // Should this be a Call Option?
   265  func RequestTimeout(d time.Duration) Option {
   266  	return func(o *Options) {
   267  		o.CallOptions.RequestTimeout = d
   268  	}
   269  }
   270  
   271  // StreamTimeout sets the stream timeout
   272  func StreamTimeout(d time.Duration) Option {
   273  	return func(o *Options) {
   274  		o.CallOptions.StreamTimeout = d
   275  	}
   276  }
   277  
   278  // Transport dial timeout
   279  func DialTimeout(d time.Duration) Option {
   280  	return func(o *Options) {
   281  		o.CallOptions.DialTimeout = d
   282  	}
   283  }
   284  
   285  // Call Options
   286  
   287  // WithExchange sets the exchange to route a message through
   288  func WithExchange(e string) PublishOption {
   289  	return func(o *PublishOptions) {
   290  		o.Exchange = e
   291  	}
   292  }
   293  
   294  // PublishContext sets the context in publish options
   295  func PublishContext(ctx context.Context) PublishOption {
   296  	return func(o *PublishOptions) {
   297  		o.Context = ctx
   298  	}
   299  }
   300  
   301  // WithAddress sets the remote addresses to use rather than using service discovery
   302  func WithAddress(a ...string) CallOption {
   303  	return func(o *CallOptions) {
   304  		o.Address = a
   305  	}
   306  }
   307  
   308  // WithCallWrapper is a CallOption which adds to the existing CallFunc wrappers
   309  func WithCallWrapper(cw ...CallWrapper) CallOption {
   310  	return func(o *CallOptions) {
   311  		o.CallWrappers = append(o.CallWrappers, cw...)
   312  	}
   313  }
   314  
   315  // WithBackoff is a CallOption which overrides that which
   316  // set in Options.CallOptions
   317  func WithBackoff(fn BackoffFunc) CallOption {
   318  	return func(o *CallOptions) {
   319  		o.Backoff = fn
   320  	}
   321  }
   322  
   323  // WithRetry is a CallOption which overrides that which
   324  // set in Options.CallOptions
   325  func WithRetry(fn RetryFunc) CallOption {
   326  	return func(o *CallOptions) {
   327  		o.Retry = fn
   328  	}
   329  }
   330  
   331  // WithRetries is a CallOption which overrides that which
   332  // set in Options.CallOptions
   333  func WithRetries(i int) CallOption {
   334  	return func(o *CallOptions) {
   335  		o.Retries = i
   336  	}
   337  }
   338  
   339  // WithRequestTimeout is a CallOption which overrides that which
   340  // set in Options.CallOptions
   341  func WithRequestTimeout(d time.Duration) CallOption {
   342  	return func(o *CallOptions) {
   343  		o.RequestTimeout = d
   344  	}
   345  }
   346  
   347  // WithStreamTimeout sets the stream timeout
   348  func WithStreamTimeout(d time.Duration) CallOption {
   349  	return func(o *CallOptions) {
   350  		o.StreamTimeout = d
   351  	}
   352  }
   353  
   354  // WithDialTimeout is a CallOption which overrides that which
   355  // set in Options.CallOptions
   356  func WithDialTimeout(d time.Duration) CallOption {
   357  	return func(o *CallOptions) {
   358  		o.DialTimeout = d
   359  	}
   360  }
   361  
   362  // WithAuthToken is a CallOption which overrides the
   363  // authorization header with the services own auth token
   364  func WithAuthToken() CallOption {
   365  	return func(o *CallOptions) {
   366  		o.AuthToken = true
   367  	}
   368  }
   369  
   370  // WithNetwork is a CallOption which sets the network attribute
   371  func WithNetwork(n string) CallOption {
   372  	return func(o *CallOptions) {
   373  		o.Network = n
   374  	}
   375  }
   376  
   377  // WithRouter sets the router to use for this call
   378  func WithRouter(r router.Router) CallOption {
   379  	return func(o *CallOptions) {
   380  		o.Router = r
   381  	}
   382  }
   383  
   384  // WithSelector sets the selector to use for this call
   385  func WithSelector(s selector.Selector) CallOption {
   386  	return func(o *CallOptions) {
   387  		o.Selector = s
   388  	}
   389  }
   390  
   391  // WithSelectOptions sets the options to pass to the selector for this call
   392  func WithSelectOptions(sops ...selector.SelectOption) CallOption {
   393  	return func(o *CallOptions) {
   394  		o.SelectOptions = sops
   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  }