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

     1  package broker
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  
     7  	"go-micro.dev/v5/codec"
     8  	"go-micro.dev/v5/logger"
     9  	"go-micro.dev/v5/registry"
    10  )
    11  
    12  type Options struct {
    13  	Codec codec.Marshaler
    14  
    15  	// Logger is the underlying logger
    16  	Logger logger.Logger
    17  
    18  	// Registry used for clustering
    19  	Registry registry.Registry
    20  	// Other options for implementations of the interface
    21  	// can be stored in a context
    22  	Context context.Context
    23  
    24  	// Handler executed when error happens in broker mesage
    25  	// processing
    26  	ErrorHandler Handler
    27  
    28  	TLSConfig *tls.Config
    29  	Addrs     []string
    30  	Secure    bool
    31  }
    32  
    33  type PublishOptions struct {
    34  	// Other options for implementations of the interface
    35  	// can be stored in a context
    36  	Context context.Context
    37  }
    38  
    39  type SubscribeOptions struct {
    40  
    41  	// Other options for implementations of the interface
    42  	// can be stored in a context
    43  	Context context.Context
    44  	// Subscribers with the same queue name
    45  	// will create a shared subscription where each
    46  	// receives a subset of messages.
    47  	Queue string
    48  
    49  	// AutoAck defaults to true. When a handler returns
    50  	// with a nil error the message is acked.
    51  	AutoAck bool
    52  }
    53  
    54  type Option func(*Options)
    55  
    56  type PublishOption func(*PublishOptions)
    57  
    58  // PublishContext set context.
    59  func PublishContext(ctx context.Context) PublishOption {
    60  	return func(o *PublishOptions) {
    61  		o.Context = ctx
    62  	}
    63  }
    64  
    65  type SubscribeOption func(*SubscribeOptions)
    66  
    67  func NewOptions(opts ...Option) *Options {
    68  	options := Options{
    69  		Context: context.Background(),
    70  		Logger:  logger.DefaultLogger,
    71  	}
    72  
    73  	for _, o := range opts {
    74  		o(&options)
    75  	}
    76  
    77  	return &options
    78  }
    79  
    80  func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
    81  	opt := SubscribeOptions{
    82  		AutoAck: true,
    83  	}
    84  
    85  	for _, o := range opts {
    86  		o(&opt)
    87  	}
    88  
    89  	return opt
    90  }
    91  
    92  // Addrs sets the host addresses to be used by the broker.
    93  func Addrs(addrs ...string) Option {
    94  	return func(o *Options) {
    95  		o.Addrs = addrs
    96  	}
    97  }
    98  
    99  // Codec sets the codec used for encoding/decoding used where
   100  // a broker does not support headers.
   101  func Codec(c codec.Marshaler) Option {
   102  	return func(o *Options) {
   103  		o.Codec = c
   104  	}
   105  }
   106  
   107  // DisableAutoAck will disable auto acking of messages
   108  // after they have been handled.
   109  func DisableAutoAck() SubscribeOption {
   110  	return func(o *SubscribeOptions) {
   111  		o.AutoAck = false
   112  	}
   113  }
   114  
   115  // ErrorHandler will catch all broker errors that cant be handled
   116  // in normal way, for example Codec errors.
   117  func ErrorHandler(h Handler) Option {
   118  	return func(o *Options) {
   119  		o.ErrorHandler = h
   120  	}
   121  }
   122  
   123  // Queue sets the name of the queue to share messages on.
   124  func Queue(name string) SubscribeOption {
   125  	return func(o *SubscribeOptions) {
   126  		o.Queue = name
   127  	}
   128  }
   129  
   130  func Registry(r registry.Registry) Option {
   131  	return func(o *Options) {
   132  		o.Registry = r
   133  	}
   134  }
   135  
   136  // Secure communication with the broker.
   137  func Secure(b bool) Option {
   138  	return func(o *Options) {
   139  		o.Secure = b
   140  	}
   141  }
   142  
   143  // Specify TLS Config.
   144  func TLSConfig(t *tls.Config) Option {
   145  	return func(o *Options) {
   146  		o.TLSConfig = t
   147  	}
   148  }
   149  
   150  // Logger sets the underline logger.
   151  func Logger(l logger.Logger) Option {
   152  	return func(o *Options) {
   153  		o.Logger = l
   154  	}
   155  }
   156  
   157  // SubscribeContext set context.
   158  func SubscribeContext(ctx context.Context) SubscribeOption {
   159  	return func(o *SubscribeOptions) {
   160  		o.Context = ctx
   161  	}
   162  }