Skip to content

Commit

Permalink
fix: for long chunks provide max buffer size configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
kevincobain2000 committed Jan 15, 2025
1 parent 8b4f282 commit 07ea7c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Flags struct {
MemLimit int
MSTeamsHook string
NotifyOnlyRecent bool
MaxBufferSizeMB int
Test bool
Version bool
}
Expand All @@ -41,6 +42,7 @@ sends health check ping to ms teams webhook
flag.IntVar(&f.MemLimit, "mem-limit", 256, "memory limit in MB (0 to disable)")
flag.IntVar(&f.FilePathsCap, "file-paths-cap", 100, "max number of file paths to watch")
flag.IntVar(&f.Min, "min", 1, "on minimum num of matches, it should notify")
flag.IntVar(&f.MaxBufferSizeMB, "max-buffer-size-mb", 0, "max buffer size in MB, default is 0 (not provided) for go's default 64KB")
flag.BoolVar(&f.NotifyOnlyRecent, "notify-only-recent", true, "Notify on latest file only by timestamp based on --every")
flag.BoolVar(&f.Version, "version", false, "")
flag.BoolVar(&f.Test, "test", false, `Quickly test paths or regex
Expand Down
6 changes: 6 additions & 0 deletions pkg/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Watcher struct {
lastFileSizeKey string
matchPattern string
ignorePattern string
maxBufferSizeMB int
lastLineNum int
lastFileSize int64
timestampNow string
Expand All @@ -43,6 +44,7 @@ func NewWatcher(
lastLineKey: "llk-" + filePath,
lastFileSizeKey: "llks-" + filePath,
timestampNow: now.Format("2006-01-02 15:04:05"),
maxBufferSizeMB: f.MaxBufferSizeMB,
}
if err := watcher.loadState(); err != nil {
return nil, err
Expand Down Expand Up @@ -102,6 +104,10 @@ func (w *Watcher) Scan() (*ScanResult, error) {
}

scanner := bufio.NewScanner(file)
if w.maxBufferSizeMB > 0 {
// for large lines
scanner.Buffer(make([]byte, 0, 64*1024), w.maxBufferSizeMB*1024*1024)
}
currentLineNum := 1
linesRead := 0
bytesRead := w.lastFileSize
Expand Down

0 comments on commit 07ea7c8

Please sign in to comment.