forked from st3v/go-plugins
/
options.go
82 lines (72 loc) · 2.45 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sqs
import (
"context"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/micro/go-micro/broker"
)
type sqsClientKey struct{}
type dedupFunctionKey struct{}
type groupIdFunctionKey struct{}
type maxMessagesKey struct{}
type visiblityTimeoutKey struct{}
type waitTimeSecondsKey struct{}
type StringFromMessageFunc func(m *broker.Message) string
// DeduplicationFunction sets the function used to create the deduplication string
// for a given message
func DeduplicationFunction(dedup StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, dedupFunctionKey{}, dedup)
}
}
// GroupIDFunction sets the function used to create the group ID string for a
// given message
func GroupIDFunction(groupfunc StringFromMessageFunc) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, groupIdFunctionKey{}, groupfunc)
}
}
// MaxReceiveMessages indicates how many messages a receive operation should pull
// during any single call
func MaxReceiveMessages(max int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, maxMessagesKey{}, max)
}
}
// VisibilityTimeout controls how long a message is hidden from other queue consumers
// before being put back. If a consumer does not delete the message, it will be put back
// even if it was "processed"
func VisibilityTimeout(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, visiblityTimeoutKey{}, seconds)
}
}
// WaitTimeSeconds controls the length of long polling for available messages
func WaitTimeSeconds(seconds int64) broker.SubscribeOption {
return func(o *broker.SubscribeOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, waitTimeSecondsKey{}, seconds)
}
}
// Client receives an instantiated instance of an SQS client which is used instead of initialising a new client
func Client(c *sqs.SQS) broker.Option {
return func(o *broker.Options) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, sqsClientKey{}, c)
}
}