@@ -11,15 +11,19 @@ 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
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
}
@@ -33,6 +37,24 @@ func NewPusher(addrs []string, topic string, opts ...PushOption) *Pusher {
33
37
Balancer : & kafka.LeastBytes {},
34
38
Compression : kafka .Snappy ,
35
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
+
36
58
pusher := & Pusher {
37
59
producer : producer ,
38
60
topic : topic ,
@@ -45,7 +67,7 @@ func NewPusher(addrs []string, topic string, opts ...PushOption) *Pusher {
45
67
if err := pusher .producer .WriteMessages (context .Background (), chunk ... ); err != nil {
46
68
logx .Error (err )
47
69
}
48
- }, newOptions ( opts ) ... )
70
+ }, chunkOpts ... )
49
71
50
72
return pusher
51
73
}
@@ -79,30 +101,21 @@ func (p *Pusher) Push(v string) error {
79
101
80
102
// WithChunkSize customizes the Pusher with the given chunk size.
81
103
func WithChunkSize (chunkSize int ) PushOption {
82
- return func (options * chunkOptions ) {
104
+ return func (options * pushOptions ) {
83
105
options .chunkSize = chunkSize
84
106
}
85
107
}
86
108
87
109
// WithFlushInterval customizes the Pusher with the given flush interval.
88
110
func WithFlushInterval (interval time.Duration ) PushOption {
89
- return func (options * chunkOptions ) {
111
+ return func (options * pushOptions ) {
90
112
options .flushInterval = interval
91
113
}
92
114
}
93
115
94
- func newOptions (opts []PushOption ) []executors.ChunkOption {
95
- var options chunkOptions
96
- for _ , opt := range opts {
97
- opt (& options )
98
- }
99
-
100
- var chunkOpts []executors.ChunkOption
101
- if options .chunkSize > 0 {
102
- chunkOpts = append (chunkOpts , executors .WithChunkBytes (options .chunkSize ))
103
- }
104
- if options .flushInterval > 0 {
105
- 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
106
120
}
107
- return chunkOpts
108
121
}
0 commit comments