Skip to content

Commit

Permalink
Merge pull request #40 from shogo82148/fix-buffer-full-error
Browse files Browse the repository at this point in the history
fix: reading files failed: bufio: buffer full
  • Loading branch information
shogo82148 committed Nov 9, 2022
2 parents 72e02c0 + ee9614e commit 6a99423
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
24 changes: 13 additions & 11 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func NewTailReaderWithOptions(reader io.Reader, opts Options) (*Tail, error) {
}
t := &tail{
parent: parent,
reader: bufio.NewReader(r),
reader: parent.newReader(r),
ctx: ctx,
cancel: cancel,
}
Expand Down Expand Up @@ -151,12 +151,6 @@ func (t *Tail) wait() {
// open opens the target file.
// If it does not exist, wait for creating new file.
func (t *Tail) open(seek int) (*tail, error) {
const defaultBufSize = 4096
bufSize := defaultBufSize
if t.opts.MaxBytesLine != 0 && int64(bufSize) > t.opts.MaxBytesLine {
bufSize = int(t.opts.MaxBytesLine)
}

watcher, err := fsnotify.NewWatcher()
if err != nil {
return nil, err
Expand Down Expand Up @@ -184,7 +178,7 @@ func (t *Tail) open(seek int) (*tail, error) {
return &tail{
parent: t,
file: file,
reader: bufio.NewReaderSize(r, bufSize),
reader: t.newReader(r),
watcher: watcher,
ctx: ctx,
cancel: cancel,
Expand All @@ -203,6 +197,15 @@ func (t *Tail) open(seek int) (*tail, error) {
}
}

func (t *Tail) newReader(r io.Reader) *bufio.Reader {
const defaultBufSize = 4096
bufSize := defaultBufSize
if t.opts.MaxBytesLine != 0 && int64(bufSize) > t.opts.MaxBytesLine {
bufSize = int(t.opts.MaxBytesLine)
}
return bufio.NewReaderSize(r, bufSize)
}

// runFile tails target files
func (t *Tail) runFile(seek int) {
child, err := t.open(seek)
Expand Down Expand Up @@ -366,14 +369,13 @@ func (t *tail) tail() error {
for {
line, err := t.reader.ReadSlice('\n')
t.buf.Write(line)
if err == bufio.ErrBufferFull {
if errors.Is(err, bufio.ErrBufferFull) {
// the reader cannot find EOL in its buffer.
// continue to read a line.
if opts.MaxBytesLine == 0 || int64(t.buf.Len()) < opts.MaxBytesLine {
continue
}
}
if err != nil {
} else if err != nil {
return err
}
t.parent.lines <- &Line{t.buf.String(), time.Now()}
Expand Down
17 changes: 14 additions & 3 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,20 @@ func TestLineLimit(t *testing.T) {
defer reader.Close()
defer tail.Close()

for line := range tail.Lines {
if len(line.Text) != 1024 {
t.Errorf("unexpected length: %d", len(line.Text))
LOOP:
for {
select {
case line, ok := <-tail.Lines:
if !ok {
break LOOP
}
if len(line.Text) != 1024 {
t.Errorf("unexpected length: %d", len(line.Text))
}
case err := <-tail.Errors:
if err != nil {
t.Error(err)
}
}
}
}

0 comments on commit 6a99423

Please sign in to comment.