Skip to content

Commit

Permalink
Cleanup file handling code
Browse files Browse the repository at this point in the history
We no longer rely on runtime.GC() for closing file,
 at least when analyzing
  • Loading branch information
iBug committed Sep 5, 2024
1 parent 0e455fe commit 3e8232a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
28 changes: 15 additions & 13 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,32 +49,34 @@ func runWithConfig(cmd *cobra.Command, args []string, config analyze.AnalyzerCon
}
}()

iterator, err := analyzer.OpenFileIterator(filenames[0])
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
var errCh chan error
if !config.Analyze {
go func() {
errCh <- analyzer.TailFile(filenames[0])
}()

if !config.Analyze && !config.Daemon {
go tui.New(analyzer).Run()
if !config.Daemon {
go tui.New(analyzer).Run()
}
} else {
go func() {
errCh <- analyzer.AnalyzeFile(filenames[0])
}()
}

if config.Daemon {
if err := systemd.NotifyReady(); err != nil {
return fmt.Errorf("failed to notify systemd: %w", err)
}
}

err = analyzer.RunLoop(iterator)
err = <-errCh

for i := 1; i < len(filenames); i++ {
if err != nil {
break
}
iterator, err = analyzer.OpenFileIterator(filenames[i])
if err != nil {
err = fmt.Errorf("failed to open file: %w", err)
break
}
err = analyzer.RunLoop(iterator)
err = analyzer.AnalyzeFile(filenames[i])
}

if config.Analyze {
Expand Down
17 changes: 17 additions & 0 deletions pkg/analyze/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ func (a *Analyzer) RunLoop(iter fileiter.Iterator) error {
return nil
}

func (a *Analyzer) AnalyzeFile(filename string) error {
f, err := os.Open(filename)
if err != nil {
return err
}
defer f.Close()
return a.RunLoop(fileiter.NewWithScanner(f))
}

func (a *Analyzer) TailFile(filename string) error {
iter, err := a.OpenTailIterator(filename)
if err != nil {
return err
}
return a.RunLoop(iter)
}

func (a *Analyzer) handleLine(line []byte) error {
logItem, err := a.logParser.Parse(line)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions pkg/analyze/analyze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ func benchmarkAnalyzeLoop(b *testing.B, parserStr string) {
b.Fatal(err)
}

t, err := a.OpenFileIterator(logPath)
err = a.AnalyzeFile(logPath)
if err != nil {
b.Fatal(err)
}

a.RunLoop(t)
a.PrintTopValues(nil, SortBySize, "")
}

Expand Down
13 changes: 0 additions & 13 deletions pkg/analyze/file.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package analyze

import (
"bufio"
"io"
"os"

Expand Down Expand Up @@ -56,15 +55,3 @@ func (a *Analyzer) OpenTailIterator(filename string) (fileiter.Iterator, error)
}
return fileiter.NewWithTail(t), nil
}

func (a *Analyzer) OpenFileIterator(filename string) (fileiter.Iterator, error) {
if !a.Config.Analyze {
return a.OpenTailIterator(filename)
} else {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
return fileiter.NewWithScanner(bufio.NewScanner(file)), nil
}
}
4 changes: 3 additions & 1 deletion pkg/fileiter/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package fileiter

import (
"bufio"
"io"

"github.com/nxadm/tail"
)
Expand All @@ -14,8 +15,9 @@ type scannerIterator struct {
scanner *bufio.Scanner
}

func NewWithScanner(scanner *bufio.Scanner) Iterator {
func NewWithScanner(r io.Reader) Iterator {
// Prepare a large buffer
scanner := bufio.NewScanner(r)
scanner.Buffer(make([]byte, 1024*1024), 1024*1024)
return &scannerIterator{scanner: scanner}
}
Expand Down

0 comments on commit 3e8232a

Please sign in to comment.