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