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