simplified nats Option injection via Context
This commit is contained in:
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user