/
options.go
52 lines (40 loc) · 1.29 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
package kafka
import (
"context"
"github.com/IBM/sarama"
"github.com/lengocson131002/go-clean-core/transport/broker"
)
var (
DefaultBrokerConfig = sarama.NewConfig()
DefaultClusterConfig = sarama.NewConfig()
)
type brokerConfigKey struct{}
type clusterConfigKey struct{}
func BrokerConfig(c *sarama.Config) broker.BrokerOption {
return setBrokerOption(brokerConfigKey{}, c)
}
func ClusterConfig(c *sarama.Config) broker.BrokerOption {
return setBrokerOption(clusterConfigKey{}, c)
}
type subscribeContextKey struct{}
// SubscribeContext set the context for broker.SubscribeOption
func SubscribeContext(ctx context.Context) broker.SubscribeOption {
return setSubscribeOption(subscribeContextKey{}, ctx)
}
type subscribeConfigKey struct{}
func SubscribeConfig(c *sarama.Config) broker.SubscribeOption {
return setSubscribeOption(subscribeConfigKey{}, c)
}
type asyncProduceErrorKey struct{}
type asyncProduceSuccessKey struct{}
func AsyncProducer(errors chan<- *sarama.ProducerError, successes chan<- *sarama.ProducerMessage) broker.BrokerOption {
// set default opt
var opt = func(options *broker.BrokerOptions) {}
if successes != nil {
opt = setBrokerOption(asyncProduceSuccessKey{}, successes)
}
if errors != nil {
opt = setBrokerOption(asyncProduceErrorKey{}, errors)
}
return opt
}