-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
57 lines (47 loc) · 1.25 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
package kafka
import (
"context"
"github.com/nextmicro/next/broker"
"github.com/IBM/sarama"
)
const (
namespace = "kafka"
)
type (
publishConfigKey struct{}
subscribeConfigKey struct{}
publishMessageKey struct{}
SendMessageResponse struct {
partition int32
offset int64
}
)
// setBrokerOption returns a function to setup a context with given value
func setBrokerOption(k, v interface{}) broker.Option {
return func(o *broker.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{}) broker.PublishOption {
return func(o *broker.PublishOptions) {
if o.Context == nil {
o.Context = context.Background()
}
o.Context = context.WithValue(o.Context, k, v)
}
}
func PublishConfig(c *sarama.Config) broker.Option {
return setBrokerOption(publishConfigKey{}, c)
}
func SubscribeConfig(c *sarama.Config) broker.Option {
return setBrokerOption(subscribeConfigKey{}, c)
}
// Key The partitioning key for this message. Pre-existing Encoders include
// StringEncoder.
func Key(key string) broker.PublishOption {
return setPublishOption(publishMessageKey{}, key)
}