@@ -52,6 +52,7 @@ type Reader struct {
52
52
includeFileRecordNum bool
53
53
compression string
54
54
acquireFSLock bool
55
+ maxBatchSize int
55
56
}
56
57
57
58
// ReadToEnd will read until the end of the file
@@ -179,6 +180,8 @@ func (r *Reader) readContents(ctx context.Context) {
179
180
// Create the scanner to read the contents of the file.
180
181
s := scanner .New (r , r .maxLogSize , r .initialBufferSize , r .Offset , r .contentSplitFunc )
181
182
183
+ tokens := make ([]emit.Token , 0 , r .maxBatchSize )
184
+
182
185
// Iterate over the contents of the file.
183
186
for {
184
187
select {
@@ -194,7 +197,7 @@ func (r *Reader) readContents(ctx context.Context) {
194
197
} else if r .deleteAtEOF {
195
198
r .delete ()
196
199
}
197
- return
200
+ break
198
201
}
199
202
200
203
token , err := r .decoder .Decode (s .Bytes ())
@@ -209,15 +212,51 @@ func (r *Reader) readContents(ctx context.Context) {
209
212
r .FileAttributes [attrs .LogFileRecordNumber ] = r .RecordNum
210
213
}
211
214
212
- err = r .emitFunc (ctx , emit .NewToken (token , r .FileAttributes ))
213
- if err != nil {
214
- r .set .Logger .Error ("failed to process token" , zap .Error (err ))
215
+ tokens = append (tokens , emit .NewToken (copyBody (token ), copyAttributes (r .FileAttributes )))
216
+
217
+ if r .maxBatchSize > 0 && len (tokens ) >= r .maxBatchSize {
218
+ for _ , t := range tokens {
219
+ err := r .emitFunc (ctx , t )
220
+ if err != nil {
221
+ r .set .Logger .Error ("failed to process token" , zap .Error (err ))
222
+ }
223
+ }
224
+ tokens = tokens [:0 ]
225
+ r .Offset = s .Pos ()
215
226
}
227
+ }
216
228
229
+ if len (tokens ) > 0 {
230
+ for _ , t := range tokens {
231
+ err := r .emitFunc (ctx , t )
232
+ if err != nil {
233
+ r .set .Logger .Error ("failed to process token" , zap .Error (err ))
234
+ }
235
+ }
217
236
r .Offset = s .Pos ()
218
237
}
219
238
}
220
239
240
+ func copyBody (body []byte ) []byte {
241
+ if body == nil {
242
+ return nil
243
+ }
244
+ copied := make ([]byte , len (body ))
245
+ copy (copied , body )
246
+ return copied
247
+ }
248
+
249
+ func copyAttributes (attrs map [string ]any ) map [string ]any {
250
+ if attrs == nil {
251
+ return nil
252
+ }
253
+ copied := make (map [string ]any , len (attrs ))
254
+ for k , v := range attrs {
255
+ copied [k ] = v
256
+ }
257
+ return copied
258
+ }
259
+
221
260
// Delete will close and delete the file
222
261
func (r * Reader ) delete () {
223
262
r .close ()
0 commit comments