forked from st3v/go-plugins
/
options.go
64 lines (55 loc) · 1.59 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package nsq
import (
"context"
"time"
"github.com/micro/go-micro/broker"
nsq "github.com/nsqio/go-nsq"
)
type concurrentHandlerKey struct{}
type maxInFlightKey struct{}
type asyncPublishKey struct{}
type deferredPublishKey struct{}
type lookupdAddrsKey struct{}
type consumerOptsKey struct{}
func WithConcurrentHandlers(n int) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, concurrentHandlerKey{}, n)
}
}
func WithMaxInFlight(n int) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxInFlightKey{}, n)
}
}
func WithAsyncPublish(doneChan chan *nsq.ProducerTransaction) broker.PublishOption {
return func(o *broker.PublishOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, asyncPublishKey{}, doneChan)
}
}
func WithDeferredPublish(delay time.Duration) broker.PublishOption {
return func(o *broker.PublishOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, deferredPublishKey{}, delay)
}
}
func WithLookupdAddrs(addrs []string) broker.Option {
return func(o *broker.Options) {
o.Context = context.WithValue(o.Context, lookupdAddrsKey{}, addrs)
}
}
func WithConsumerOpts(consumerOpts []string) broker.Option {
return func(o *broker.Options) {
o.Context = context.WithValue(o.Context, consumerOptsKey{}, consumerOpts)
}
}