gitee.com/liuxuezhan/go-micro-v1.18.0@v1.0.0/broker/options.go (about)

     1  package broker
     2  
     3  import (
     4  	"context"
     5  	"crypto/tls"
     6  
     7  	"gitee.com/liuxuezhan/go-micro-v1.18.0/codec"
     8  	"gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
     9  )
    10  
    11  type Options struct {
    12  	Addrs     []string
    13  	Secure    bool
    14  	Codec     codec.Marshaler
    15  	TLSConfig *tls.Config
    16  	// Other options for implementations of the interface
    17  	// can be stored in a context
    18  	Context context.Context
    19  }
    20  
    21  type PublishOptions struct {
    22  	// Other options for implementations of the interface
    23  	// can be stored in a context
    24  	Context context.Context
    25  }
    26  
    27  type SubscribeOptions struct {
    28  	// AutoAck defaults to true. When a handler returns
    29  	// with a nil error the message is acked.
    30  	AutoAck bool
    31  	// Subscribers with the same queue name
    32  	// will create a shared subscription where each
    33  	// receives a subset of messages.
    34  	Queue string
    35  
    36  	// Other options for implementations of the interface
    37  	// can be stored in a context
    38  	Context context.Context
    39  }
    40  
    41  type Option func(*Options)
    42  
    43  type PublishOption func(*PublishOptions)
    44  
    45  type SubscribeOption func(*SubscribeOptions)
    46  
    47  var (
    48  	registryKey = "gitee.com/liuxuezhan/go-micro-v1.18.0/registry"
    49  )
    50  
    51  func NewSubscribeOptions(opts ...SubscribeOption) SubscribeOptions {
    52  	opt := SubscribeOptions{
    53  		AutoAck: true,
    54  	}
    55  
    56  	for _, o := range opts {
    57  		o(&opt)
    58  	}
    59  
    60  	return opt
    61  }
    62  
    63  // Addrs sets the host addresses to be used by the broker
    64  func Addrs(addrs ...string) Option {
    65  	return func(o *Options) {
    66  		o.Addrs = addrs
    67  	}
    68  }
    69  
    70  // Codec sets the codec used for encoding/decoding used where
    71  // a broker does not support headers
    72  func Codec(c codec.Marshaler) Option {
    73  	return func(o *Options) {
    74  		o.Codec = c
    75  	}
    76  }
    77  
    78  // DisableAutoAck will disable auto acking of messages
    79  // after they have been handled.
    80  func DisableAutoAck() SubscribeOption {
    81  	return func(o *SubscribeOptions) {
    82  		o.AutoAck = false
    83  	}
    84  }
    85  
    86  // Queue sets the name of the queue to share messages on
    87  func Queue(name string) SubscribeOption {
    88  	return func(o *SubscribeOptions) {
    89  		o.Queue = name
    90  	}
    91  }
    92  
    93  func Registry(r registry.Registry) Option {
    94  	return func(o *Options) {
    95  		o.Context = context.WithValue(o.Context, registryKey, r)
    96  	}
    97  }
    98  
    99  // Secure communication with the broker
   100  func Secure(b bool) Option {
   101  	return func(o *Options) {
   102  		o.Secure = b
   103  	}
   104  }
   105  
   106  // Specify TLS Config
   107  func TLSConfig(t *tls.Config) Option {
   108  	return func(o *Options) {
   109  		o.TLSConfig = t
   110  	}
   111  }
   112  
   113  // SubscribeContext set context
   114  func SubscribeContext(ctx context.Context) SubscribeOption {
   115  	return func(o *SubscribeOptions) {
   116  		o.Context = ctx
   117  	}
   118  }