github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/broker/config.go (about) 1 package broker 2 3 import ( 4 "context" 5 "crypto/tls" 6 "strings" 7 "time" 8 9 "github.com/volts-dev/volts/codec" 10 "github.com/volts-dev/volts/config" 11 "github.com/volts-dev/volts/logger" 12 "github.com/volts-dev/volts/registry" 13 ) 14 15 var log = logger.New("broker") 16 17 type ( 18 Option func(*Config) 19 PublishOption func(*PublishConfig) 20 SubscribeOption func(*SubscribeConfig) 21 22 Config struct { 23 config.Config `field:"-"` 24 ErrorHandler Handler `field:"-"` 25 Codec codec.ICodec `field:"-"` // Codec 26 Registry registry.IRegistry `field:"-"` // Registry used for clustering 27 Context context.Context `field:"-"` 28 TLSConfig *tls.Config `field:"-"` 29 PrefixName string `field:"-"` 30 Name string `field:"-"` 31 Addrs []string 32 33 // Handler executed when error happens in broker mesage 34 // processing 35 Secure bool 36 37 BroadcastVersion string 38 RegisterTTL time.Duration `field:"register_ttl"` 39 RegisterInterval time.Duration 40 41 // Other options for implementations of the interface 42 // can be stored in a context 43 } 44 45 PublishConfig struct { 46 Codec codec.ICodec 47 SerializeType codec.SerializeType 48 // Other options for implementations of the interface 49 // can be stored in a context 50 Context context.Context 51 } 52 53 SubscribeConfig struct { 54 // AutoAck defaults to true. When a handler returns 55 // with a nil error the message is acked. 56 AutoAck bool 57 // Subscribers with the same queue name 58 // will create a shared subscription where each 59 // receives a subset of messages. 60 Queue string 61 62 // Other options for implementations of the interface 63 // can be stored in a context 64 Context context.Context 65 } 66 ) 67 68 var ( 69 DefaultAddress = "127.0.0.1:0" 70 ) 71 72 func NewConfig(opts ...Option) *Config { 73 cfg := &Config{ 74 //Config: config.Default(), 75 //Name: "broker", 76 BroadcastVersion: "broadcast", 77 RegisterTTL: time.Minute, 78 RegisterInterval: time.Second * 30, 79 } 80 cfg.Init(opts...) 81 config.Register(cfg) 82 return cfg 83 } 84 85 func (self *Config) String() string { 86 if len(self.PrefixName) > 0 { 87 return strings.Join([]string{self.PrefixName, "broker"}, ".") 88 } 89 90 if len(self.Name) > 0 { 91 return strings.Join([]string{"broker", self.Name}, ".") 92 } 93 94 return "" 95 } 96 97 func (self *Config) Init(opts ...Option) { 98 for _, opt := range opts { 99 if opt != nil { 100 opt(self) 101 } 102 } 103 } 104 105 func (self *Config) Load() error { 106 return self.LoadToModel(self) 107 } 108 109 func (self *Config) Save(immed ...bool) error { 110 return self.SaveFromModel(self, immed...) 111 } 112 113 func Logger() logger.ILogger { 114 return log 115 } 116 117 func NewSubscribeOptions(opts ...SubscribeOption) *SubscribeConfig { 118 cfg := &SubscribeConfig{ 119 AutoAck: true, 120 } 121 122 for _, opt := range opts { 123 opt(cfg) 124 } 125 126 return cfg 127 } 128 129 // Addrs sets the host addresses to be used by the broker. 130 func Addrs(addrs ...string) Option { 131 return func(o *Config) { 132 o.Addrs = addrs 133 } 134 } 135 136 // DisableAutoAck will disable auto acking of messages 137 // after they have been handled. 138 func DisableAutoAck() SubscribeOption { 139 return func(o *SubscribeConfig) { 140 o.AutoAck = false 141 } 142 } 143 144 // ErrorHandler will catch all broker errors that cant be handled 145 // in normal way, for example Codec errors. 146 func ErrorHandler(h Handler) Option { 147 return func(o *Config) { 148 o.ErrorHandler = h 149 } 150 } 151 152 // Queue sets the name of the queue to share messages on. 153 func Queue(name string) SubscribeOption { 154 return func(o *SubscribeConfig) { 155 o.Queue = name 156 } 157 } 158 159 // 修改Config.json的路径 160 func WithName(name string) Option { 161 return func(cfg *Config) { 162 //cfg.Unregister(cfg) 163 cfg.Name = name 164 //cfg.Register(cfg) 165 } 166 } 167 168 // 修改Config.json的路径 169 func WithConfigPrefixName(prefixName string) Option { 170 return func(cfg *Config) { 171 cfg.Unregister(cfg) 172 cfg.PrefixName = prefixName 173 cfg.Register(cfg) 174 } 175 } 176 177 func WithRegisterTTL(t time.Duration) Option { 178 return func(cfg *Config) { 179 cfg.RegisterTTL = t 180 } 181 } 182 183 func WithRegisterInterval(t time.Duration) Option { 184 return func(cfg *Config) { 185 cfg.RegisterInterval = t 186 } 187 } 188 189 func WithRegistry(r registry.IRegistry) Option { 190 return func(o *Config) { 191 o.Registry = r 192 } 193 } 194 195 // Secure communication with the broker. 196 func WithSecure(b bool) Option { 197 return func(o *Config) { 198 o.Secure = b 199 } 200 } 201 202 // Specify TLS Config. 203 func WithTLSConfig(t *tls.Config) Option { 204 return func(o *Config) { 205 o.TLSConfig = t 206 } 207 } 208 209 func WithContext(ctx context.Context) Option { 210 return func(cfg *Config) { 211 cfg.Context = ctx 212 } 213 } 214 215 // Codec sets the codec used for encoding/decoding used where 216 // a broker does not support headers. 217 func WithCodec(c codec.SerializeType) Option { 218 return func(cfg *Config) { 219 cfg.Codec = codec.IdentifyCodec(c) 220 } 221 } 222 223 func WithPublishCodec(c codec.SerializeType) PublishOption { 224 return func(cfg *PublishConfig) { 225 cfg.SerializeType = c 226 cfg.Codec = codec.IdentifyCodec(c) 227 } 228 } 229 230 // PublishContext set context. 231 func PublishContext(ctx context.Context) PublishOption { 232 return func(o *PublishConfig) { 233 o.Context = ctx 234 } 235 } 236 237 // SubscribeContext set context. 238 func SubscribeContext(ctx context.Context) SubscribeOption { 239 return func(o *SubscribeConfig) { 240 o.Context = ctx 241 } 242 }