simplified nats Option injection via Context
This commit is contained in:
parent
45eff71f26
commit
1bacad1fe8
19
nats.go
19
nats.go
@ -170,38 +170,39 @@ func (n *nbroker) String() string {
|
||||
|
||||
func NewBroker(opts ...broker.Option) broker.Broker {
|
||||
|
||||
bopts := &brokerOptions{
|
||||
natsOptions: DefaultNatsOptions,
|
||||
}
|
||||
|
||||
options := broker.Options{
|
||||
// Default codec
|
||||
Codec: json.NewCodec(),
|
||||
Context: context.WithValue(context.Background(), optionsKey, bopts),
|
||||
Context: context.Background(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
natsOptions := nats.GetDefaultOptions()
|
||||
if n, ok := options.Context.Value(optionsKey{}).(nats.Options); ok {
|
||||
natsOptions = n
|
||||
}
|
||||
|
||||
// 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
|
||||
options.Addrs = natsOptions.Servers
|
||||
}
|
||||
|
||||
if !options.Secure {
|
||||
options.Secure = bopts.natsOptions.Secure
|
||||
options.Secure = natsOptions.Secure
|
||||
}
|
||||
|
||||
if options.TLSConfig == nil {
|
||||
options.TLSConfig = bopts.natsOptions.TLSConfig
|
||||
options.TLSConfig = natsOptions.TLSConfig
|
||||
}
|
||||
|
||||
nb := &nbroker{
|
||||
opts: options,
|
||||
nopts: bopts.natsOptions,
|
||||
nopts: natsOptions,
|
||||
addrs: setAddrs(options.Addrs),
|
||||
}
|
||||
|
||||
|
@ -67,7 +67,7 @@ func TestInitAddrs(t *testing.T) {
|
||||
case "natsOptionConstructor":
|
||||
nopts := nats.GetDefaultOptions()
|
||||
nopts.Servers = addrs
|
||||
br = NewBroker(NatsOptions(nopts))
|
||||
br = NewBroker(Options(nopts))
|
||||
br.Init()
|
||||
case "default":
|
||||
br = NewBroker()
|
||||
|
24
options.go
24
options.go
@ -1,27 +1,21 @@
|
||||
package nats
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/micro/go-micro/broker"
|
||||
"github.com/nats-io/nats"
|
||||
)
|
||||
|
||||
var (
|
||||
DefaultNatsOptions = nats.GetDefaultOptions()
|
||||
type optionsKey struct{}
|
||||
|
||||
optionsKey = optionsKeyType{}
|
||||
)
|
||||
|
||||
type optionsKeyType struct{}
|
||||
|
||||
type brokerOptions struct {
|
||||
natsOptions nats.Options
|
||||
}
|
||||
|
||||
// NatsOptions allow to inject a nats.Options struct for configuring
|
||||
// Options allow to inject a nats.Options struct for configuring
|
||||
// the nats connection
|
||||
func NatsOptions(nopts nats.Options) broker.Option {
|
||||
func Options(nopts nats.Options) broker.Option {
|
||||
return func(o *broker.Options) {
|
||||
no := o.Context.Value(optionsKey).(*brokerOptions)
|
||||
no.natsOptions = nopts
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, optionsKey{}, nopts)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user