Provide a way to set native SubscriptionOption for stan broker through server.SubscriberOption

This commit is contained in:
LocTD 2019-05-24 15:39:27 +07:00 committed by Vasiliy Tolstov
parent e545f8cbc4
commit de6f824e4a
2 changed files with 16 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/server"
) )
// setSubscribeOption returns a function to setup a context with given value // setSubscribeOption returns a function to setup a context with given value
@ -26,6 +27,16 @@ func setBrokerOption(k, v interface{}) broker.Option {
} }
} }
// setBrokerOption returns a function to setup a context with given value
func setServerSubscriberOption(k, v interface{}) server.SubscriberOption {
return func(o *server.SubscriberOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}
// setPublishOption returns a function to setup a context with given value // setPublishOption returns a function to setup a context with given value
func setPublishOption(k, v interface{}) broker.PublishOption { func setPublishOption(k, v interface{}) broker.PublishOption {
return func(o *broker.PublishOptions) { return func(o *broker.PublishOptions) {

View File

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/micro/go-micro/broker" "github.com/micro/go-micro/broker"
"github.com/micro/go-micro/server"
stan "github.com/nats-io/go-nats-streaming" stan "github.com/nats-io/go-nats-streaming"
) )
@ -35,6 +36,10 @@ func SubscribeOption(opts ...stan.SubscriptionOption) broker.SubscribeOption {
return setSubscribeOption(subscribeOptionKey{}, opts) return setSubscribeOption(subscribeOptionKey{}, opts)
} }
func ServerSubscriberOption(opts ...stan.SubscriptionOption) server.SubscriberOption {
return setServerSubscriberOption(subscribeOptionKey{}, opts)
}
type subscribeContextKey struct{} type subscribeContextKey struct{}
// SubscribeContext set the context for broker.SubscribeOption // SubscribeContext set the context for broker.SubscribeOption