refactor seek routine

This commit is contained in:
Sridhar Ratnakumar 2014-04-28 14:55:51 -07:00
parent 555e6044ca
commit c13cdd473c
2 changed files with 13 additions and 5 deletions

16
tail.go
View File

@ -231,13 +231,11 @@ 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
} }
// Reset the read buffer whenever the file is re-seek'ed
tail.reader.Reset(tail.file)
} }
} }
case io.EOF: case io.EOF:
@ -321,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

@ -268,7 +268,7 @@ func TestRateLimiting(_t *testing.T) {
// 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, "more", "data"}) []string{"hello", "world", "again", expecting, "more", "data"})
// Add more data only after reasonable delay. // Add more data only after reasonable delay.