From de6f824e4a4e5c71909e1b05e845892a675968f9 Mon Sep 17 00:00:00 2001 From: LocTD Date: Fri, 24 May 2019 15:39:27 +0700 Subject: [PATCH] Provide a way to set native SubscriptionOption for stan broker through server.SubscriberOption --- context.go | 11 +++++++++++ options.go | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/context.go b/context.go index 8672894..a9885a5 100644 --- a/context.go +++ b/context.go @@ -4,6 +4,7 @@ import ( "context" "github.com/micro/go-micro/broker" + "github.com/micro/go-micro/server" ) // 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 func setPublishOption(k, v interface{}) broker.PublishOption { return func(o *broker.PublishOptions) { diff --git a/options.go b/options.go index 137587e..72278d7 100644 --- a/options.go +++ b/options.go @@ -5,6 +5,7 @@ import ( "time" "github.com/micro/go-micro/broker" + "github.com/micro/go-micro/server" stan "github.com/nats-io/go-nats-streaming" ) @@ -35,6 +36,10 @@ func SubscribeOption(opts ...stan.SubscriptionOption) broker.SubscribeOption { return setSubscribeOption(subscribeOptionKey{}, opts) } +func ServerSubscriberOption(opts ...stan.SubscriptionOption) server.SubscriberOption { + return setServerSubscriberOption(subscribeOptionKey{}, opts) +} + type subscribeContextKey struct{} // SubscribeContext set the context for broker.SubscribeOption