2020-09-22 13:15:05 +03:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
)
|
|
|
|
|
|
|
|
type brokerKey struct{}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// FromContext returns broker from passed context
|
2020-09-22 13:15:05 +03:00
|
|
|
func FromContext(ctx context.Context) (Broker, bool) {
|
|
|
|
c, ok := ctx.Value(brokerKey{}).(Broker)
|
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2020-12-08 00:38:37 +03:00
|
|
|
// NewContext savess broker in context
|
2020-09-22 13:15:05 +03:00
|
|
|
func NewContext(ctx context.Context, s Broker) context.Context {
|
|
|
|
return context.WithValue(ctx, brokerKey{}, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetSubscribeOption returns a function to setup a context with given value
|
|
|
|
func SetSubscribeOption(k, v interface{}) SubscribeOption {
|
|
|
|
return func(o *SubscribeOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 13:17:38 +03:00
|
|
|
// SetOption returns a function to setup a context with given value
|
|
|
|
func SetOption(k, v interface{}) Option {
|
2020-09-22 13:15:05 +03:00
|
|
|
return func(o *Options) {
|
|
|
|
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{}) PublishOption {
|
|
|
|
return func(o *PublishOptions) {
|
|
|
|
if o.Context == nil {
|
|
|
|
o.Context = context.Background()
|
|
|
|
}
|
|
|
|
o.Context = context.WithValue(o.Context, k, v)
|
|
|
|
}
|
|
|
|
}
|