Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Apr, 2014

* LimitRate now discards read buffer (PR #28)
* allow reading of longer lines if MaxLineSize is unset (PR #24)
* updated deps.json to latest fsnotify (441bbc86b1)

Expand Down
14 changes: 12 additions & 2 deletions tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,9 @@ func (tail *Tail) tailFileSync() {
case <-tail.Dying():
return
}
_, err := tail.file.Seek(0, 2) // Seek to fine end
err = tail.seekEnd()
if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err)
tail.Kill(err)
return
}
}
Expand Down Expand Up @@ -319,6 +319,16 @@ func (tail *Tail) newReader() *bufio.Reader {
}
}

func (tail *Tail) seekEnd() error {
_, err := tail.file.Seek(0, 2)
if err != nil {
return fmt.Errorf("Seek error on %s: %s", tail.Filename, err)
}
// Reset the read buffer whenever the file is re-seek'ed
tail.reader.Reset(tail.file)
return nil
}

// sendLine sends the line(s) to Lines channel, splitting longer lines
// if necessary. Return false if rate limit is reached.
func (tail *Tail) sendLine(line []byte) bool {
Expand Down
15 changes: 11 additions & 4 deletions tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,22 @@ func TestRateLimiting(_t *testing.T) {
LimitRate: 2}
expecting := "Too much log activity (more than 2 lines per second being written); waiting a second before resuming tailing"
tail := t.StartTail("test.txt", config)

// TODO: also verify that tail resumes after the cooloff period.
go t.VerifyTailOutput(
tail,
[]string{"hello", "world", "again", expecting})
tail,
[]string{"hello", "world", "again", expecting, "more", "data"})

// Add more data only after reasonable delay.
<-time.After(1200 * time.Millisecond)
t.AppendFile("test.txt", "more\ndata\n")

// Delete after a reasonable delay, to give tail sufficient time
// to read all lines.
<-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt")
tail.Stop()

// tail.Stop()
Cleanup()
}

Expand Down Expand Up @@ -393,9 +399,10 @@ func (t TailTest) VerifyTailOutput(tail *Tail, lines []string) {
for idx, line := range lines {
tailedLine, ok := <-tail.Lines
if !ok {
// tail.Lines is closed and empty.
err := tail.Err()
if err != nil {
t.Errorf("tail ended with error: %v", err)
t.Fatalf("tail ended with error: %v", err)
}
t.Fatalf("tail ended early; expecting more: %v", lines[idx:])
}
Expand Down