@@ -11,92 +11,111 @@ import (
11
11
)
12
12
13
13
type (
14
- PushOption func (options * chunkOptions )
14
+ PushOption func (options * pushOptions )
15
15
16
16
Pusher struct {
17
- produer * kafka.Writer
17
+ producer * kafka.Writer
18
18
topic string
19
19
executor * executors.ChunkExecutor
20
20
}
21
21
22
- chunkOptions struct {
22
+ pushOptions struct {
23
+ // kafka.Writer options
24
+ allowAutoTopicCreation bool
25
+
26
+ // executors.ChunkExecutor options
23
27
chunkSize int
24
28
flushInterval time.Duration
25
29
}
26
30
)
27
31
32
+ // NewPusher returns a Pusher with the given Kafka addresses and topic.
28
33
func NewPusher (addrs []string , topic string , opts ... PushOption ) * Pusher {
29
34
producer := & kafka.Writer {
30
35
Addr : kafka .TCP (addrs ... ),
31
36
Topic : topic ,
32
37
Balancer : & kafka.LeastBytes {},
33
38
Compression : kafka .Snappy ,
34
39
}
40
+
41
+ var options pushOptions
42
+ for _ , opt := range opts {
43
+ opt (& options )
44
+ }
45
+
46
+ // apply kafka.Writer options
47
+ producer .AllowAutoTopicCreation = options .allowAutoTopicCreation
48
+
49
+ // apply ChunkExecutor options
50
+ var chunkOpts []executors.ChunkOption
51
+ if options .chunkSize > 0 {
52
+ chunkOpts = append (chunkOpts , executors .WithChunkBytes (options .chunkSize ))
53
+ }
54
+ if options .flushInterval > 0 {
55
+ chunkOpts = append (chunkOpts , executors .WithFlushInterval (options .flushInterval ))
56
+ }
57
+
35
58
pusher := & Pusher {
36
- produer : producer ,
37
- topic : topic ,
59
+ producer : producer ,
60
+ topic : topic ,
38
61
}
39
62
pusher .executor = executors .NewChunkExecutor (func (tasks []interface {}) {
40
63
chunk := make ([]kafka.Message , len (tasks ))
41
64
for i := range tasks {
42
65
chunk [i ] = tasks [i ].(kafka.Message )
43
66
}
44
- if err := pusher .produer .WriteMessages (context .Background (), chunk ... ); err != nil {
67
+ if err := pusher .producer .WriteMessages (context .Background (), chunk ... ); err != nil {
45
68
logx .Error (err )
46
69
}
47
- }, newOptions ( opts ) ... )
70
+ }, chunkOpts ... )
48
71
49
72
return pusher
50
73
}
51
74
75
+ // Close closes the Pusher and releases any resources used by it.
52
76
func (p * Pusher ) Close () error {
53
77
if p .executor != nil {
54
78
p .executor .Flush ()
55
79
}
56
-
57
- return p .produer .Close ()
80
+
81
+ return p .producer .Close ()
58
82
}
59
83
84
+ // Name returns the name of the Kafka topic that the Pusher is sending messages to.
60
85
func (p * Pusher ) Name () string {
61
86
return p .topic
62
87
}
63
88
89
+ // Push sends a message to the Kafka topic.
64
90
func (p * Pusher ) Push (v string ) error {
65
91
msg := kafka.Message {
66
- Key : []byte (strconv .FormatInt (time .Now ().UnixNano (), 10 )),
92
+ Key : []byte (strconv .FormatInt (time .Now ().UnixNano (), 10 )), // current timestamp
67
93
Value : []byte (v ),
68
94
}
69
95
if p .executor != nil {
70
96
return p .executor .Add (msg , len (v ))
71
97
} else {
72
- return p .produer .WriteMessages (context .Background (), msg )
98
+ return p .producer .WriteMessages (context .Background (), msg )
73
99
}
74
100
}
75
101
102
+ // WithChunkSize customizes the Pusher with the given chunk size.
76
103
func WithChunkSize (chunkSize int ) PushOption {
77
- return func (options * chunkOptions ) {
104
+ return func (options * pushOptions ) {
78
105
options .chunkSize = chunkSize
79
106
}
80
107
}
81
108
109
+ // WithFlushInterval customizes the Pusher with the given flush interval.
82
110
func WithFlushInterval (interval time.Duration ) PushOption {
83
- return func (options * chunkOptions ) {
111
+ return func (options * pushOptions ) {
84
112
options .flushInterval = interval
85
113
}
86
114
}
87
115
88
- func newOptions (opts []PushOption ) []executors.ChunkOption {
89
- var options chunkOptions
90
- for _ , opt := range opts {
91
- opt (& options )
92
- }
93
-
94
- var chunkOpts []executors.ChunkOption
95
- if options .chunkSize > 0 {
96
- chunkOpts = append (chunkOpts , executors .WithChunkBytes (options .chunkSize ))
97
- }
98
- if options .flushInterval > 0 {
99
- chunkOpts = append (chunkOpts , executors .WithFlushInterval (options .flushInterval ))
116
+ // WithAllowAutoTopicCreation allows the Pusher to create the given topic if it does not exist.
117
+ func WithAllowAutoTopicCreation () PushOption {
118
+ return func (options * pushOptions ) {
119
+ options .allowAutoTopicCreation = true
100
120
}
101
- return chunkOpts
102
121
}
0 commit comments