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) {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
return nil, false
|
|
|
|
}
|
2020-09-22 13:15:05 +03:00
|
|
|
c, ok := ctx.Value(brokerKey{}).(Broker)
|
|
|
|
return c, ok
|
|
|
|
}
|
|
|
|
|
2024-12-18 01:31:21 +03:00
|
|
|
// MustContext returns broker from passed context
|
|
|
|
func MustContext(ctx context.Context) Broker {
|
|
|
|
b, ok := FromContext(ctx)
|
|
|
|
if !ok {
|
|
|
|
panic("missing broker")
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-12-17 22:52:00 +03:00
|
|
|
if ctx == nil {
|
|
|
|
ctx = context.Background()
|
|
|
|
}
|
2020-09-22 13:15:05 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|