diff --git a/CHANGES.md b/CHANGES.md index 498fb58..617a67f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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) diff --git a/tail.go b/tail.go index 34ef4cc..482ca88 100644 --- a/tail.go +++ b/tail.go @@ -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 } } @@ -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 { diff --git a/tail_test.go b/tail_test.go index ab3bfbe..58b340a 100644 --- a/tail_test.go +++ b/tail_test.go @@ -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() } @@ -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:]) }