Merge pull request #28 from ActiveState/rate_limit_handle_buffer

LimitRate should discard read buffer

internal Bug #103143
This commit is contained in:
Sridhar Ratnakumar 2014-04-28 15:02:21 -07:00
commit 6ca4028d41
3 changed files with 24 additions and 6 deletions

View File

@ -1,5 +1,6 @@
# Apr, 2014 # Apr, 2014
* LimitRate now discards read buffer (PR #28)
* allow reading of longer lines if MaxLineSize is unset (PR #24) * allow reading of longer lines if MaxLineSize is unset (PR #24)
* updated deps.json to latest fsnotify (441bbc86b1) * updated deps.json to latest fsnotify (441bbc86b1)

14
tail.go
View File

@ -231,9 +231,9 @@ func (tail *Tail) tailFileSync() {
case <-tail.Dying(): case <-tail.Dying():
return return
} }
_, err := tail.file.Seek(0, 2) // Seek to fine end err = tail.seekEnd()
if err != nil { if err != nil {
tail.Killf("Seek error on %s: %s", tail.Filename, err) tail.Kill(err)
return 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 // sendLine sends the line(s) to Lines channel, splitting longer lines
// if necessary. Return false if rate limit is reached. // if necessary. Return false if rate limit is reached.
func (tail *Tail) sendLine(line []byte) bool { func (tail *Tail) sendLine(line []byte) bool {

View File

@ -265,16 +265,22 @@ func TestRateLimiting(_t *testing.T) {
LimitRate: 2} LimitRate: 2}
expecting := "Too much log activity (more than 2 lines per second being written); waiting a second before resuming tailing" 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) tail := t.StartTail("test.txt", config)
// TODO: also verify that tail resumes after the cooloff period. // TODO: also verify that tail resumes after the cooloff period.
go t.VerifyTailOutput( go t.VerifyTailOutput(
tail, tail,
[]string{"hello", "world", "again", expecting}) []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 // Delete after a reasonable delay, to give tail sufficient time
// to read all lines. // to read all lines.
<-time.After(100 * time.Millisecond) <-time.After(100 * time.Millisecond)
t.RemoveFile("test.txt") t.RemoveFile("test.txt")
tail.Stop()
// tail.Stop()
Cleanup() Cleanup()
} }
@ -393,9 +399,10 @@ func (t TailTest) VerifyTailOutput(tail *Tail, lines []string) {
for idx, line := range lines { for idx, line := range lines {
tailedLine, ok := <-tail.Lines tailedLine, ok := <-tail.Lines
if !ok { if !ok {
// tail.Lines is closed and empty.
err := tail.Err() err := tail.Err()
if err != nil { 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:]) t.Fatalf("tail ended early; expecting more: %v", lines[idx:])
} }