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 {
|
func NewBroker(opts ...broker.Option) broker.Broker {
|
||||||
|
|
||||||
bopts := &brokerOptions{
|
|
||||||
natsOptions: DefaultNatsOptions,
|
|
||||||
}
|
|
||||||
|
|
||||||
options := broker.Options{
|
options := broker.Options{
|
||||||
// Default codec
|
// Default codec
|
||||||
Codec: json.NewCodec(),
|
Codec: json.NewCodec(),
|
||||||
Context: context.WithValue(context.Background(), optionsKey, bopts),
|
Context: context.Background(),
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&options)
|
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
|
// broker.Options have higher priority than nats.Options
|
||||||
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
// only if Addrs, Secure or TLSConfig were not set through a broker.Option
|
||||||
// we read them from nats.Option
|
// we read them from nats.Option
|
||||||
if len(options.Addrs) == 0 {
|
if len(options.Addrs) == 0 {
|
||||||
options.Addrs = bopts.natsOptions.Servers
|
options.Addrs = natsOptions.Servers
|
||||||
}
|
}
|
||||||
|
|
||||||
if !options.Secure {
|
if !options.Secure {
|
||||||
options.Secure = bopts.natsOptions.Secure
|
options.Secure = natsOptions.Secure
|
||||||
}
|
}
|
||||||
|
|
||||||
if options.TLSConfig == nil {
|
if options.TLSConfig == nil {
|
||||||
options.TLSConfig = bopts.natsOptions.TLSConfig
|
options.TLSConfig = natsOptions.TLSConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
nb := &nbroker{
|
nb := &nbroker{
|
||||||
opts: options,
|
opts: options,
|
||||||
nopts: bopts.natsOptions,
|
nopts: natsOptions,
|
||||||
addrs: setAddrs(options.Addrs),
|
addrs: setAddrs(options.Addrs),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ func TestInitAddrs(t *testing.T) {
|
|||||||
case "natsOptionConstructor":
|
case "natsOptionConstructor":
|
||||||
nopts := nats.GetDefaultOptions()
|
nopts := nats.GetDefaultOptions()
|
||||||
nopts.Servers = addrs
|
nopts.Servers = addrs
|
||||||
br = NewBroker(NatsOptions(nopts))
|
br = NewBroker(Options(nopts))
|
||||||
br.Init()
|
br.Init()
|
||||||
case "default":
|
case "default":
|
||||||
br = NewBroker()
|
br = NewBroker()
|
||||||
|
24
options.go
24
options.go
@ -1,27 +1,21 @@
|
|||||||
package nats
|
package nats
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
"github.com/micro/go-micro/broker"
|
"github.com/micro/go-micro/broker"
|
||||||
"github.com/nats-io/nats"
|
"github.com/nats-io/nats"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type optionsKey struct{}
|
||||||
DefaultNatsOptions = nats.GetDefaultOptions()
|
|
||||||
|
|
||||||
optionsKey = optionsKeyType{}
|
// Options allow to inject a nats.Options struct for configuring
|
||||||
)
|
|
||||||
|
|
||||||
type optionsKeyType struct{}
|
|
||||||
|
|
||||||
type brokerOptions struct {
|
|
||||||
natsOptions nats.Options
|
|
||||||
}
|
|
||||||
|
|
||||||
// NatsOptions allow to inject a nats.Options struct for configuring
|
|
||||||
// the nats connection
|
// the nats connection
|
||||||
func NatsOptions(nopts nats.Options) broker.Option {
|
func Options(nopts nats.Options) broker.Option {
|
||||||
return func(o *broker.Options) {
|
return func(o *broker.Options) {
|
||||||
no := o.Context.Value(optionsKey).(*brokerOptions)
|
if o.Context == nil {
|
||||||
no.natsOptions = nopts
|
o.Context = context.Background()
|
||||||
|
}
|
||||||
|
o.Context = context.WithValue(o.Context, optionsKey{}, nopts)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user