add option to inject nats.Options
This commit is contained in:
36
nats.go
36
nats.go
@@ -2,6 +2,7 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/micro/go-micro/broker"
|
||||
@@ -14,6 +15,7 @@ type nbroker struct {
|
||||
addrs []string
|
||||
conn *nats.Conn
|
||||
opts broker.Options
|
||||
nopts nats.Options
|
||||
}
|
||||
|
||||
type subscriber struct {
|
||||
@@ -87,7 +89,7 @@ func (n *nbroker) Connect() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
opts := nats.DefaultOptions
|
||||
opts := n.nopts
|
||||
opts.Servers = n.addrs
|
||||
opts.Secure = n.opts.Secure
|
||||
opts.TLSConfig = n.opts.TLSConfig
|
||||
@@ -166,17 +168,41 @@ func (n *nbroker) String() string {
|
||||
}
|
||||
|
||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
||||
|
||||
bopts := &brokerOptions{
|
||||
natsOptions: DefaultNatsOptions,
|
||||
}
|
||||
|
||||
options := broker.Options{
|
||||
// Default codec
|
||||
Codec: json.NewCodec(),
|
||||
Codec: json.NewCodec(),
|
||||
Context: context.WithValue(context.Background(), optionsKey, bopts),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
return &nbroker{
|
||||
addrs: setAddrs(options.Addrs),
|
||||
opts: options,
|
||||
// broker.Options have higher priority than nats.Options
|
||||
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
||||
// we read them from nats.Option
|
||||
if len(options.Addrs) == 0 {
|
||||
options.Addrs = bopts.natsOptions.Servers
|
||||
}
|
||||
|
||||
if !options.Secure {
|
||||
options.Secure = bopts.natsOptions.Secure
|
||||
}
|
||||
|
||||
if options.TLSConfig == nil {
|
||||
options.TLSConfig = bopts.natsOptions.TLSConfig
|
||||
}
|
||||
|
||||
nb := &nbroker{
|
||||
opts: options,
|
||||
nopts: bopts.natsOptions,
|
||||
addrs: setAddrs(options.Addrs),
|
||||
}
|
||||
|
||||
return nb
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user