diff --git a/pkg/lobster/tailer/tail/tail.go b/pkg/lobster/tailer/tail/tail.go index a7d22e9..0eb26a5 100644 --- a/pkg/lobster/tailer/tail/tail.go +++ b/pkg/lobster/tailer/tail/tail.go @@ -11,6 +11,7 @@ import ( "os" "strings" "sync" + "sync/atomic" "time" "github.com/golang/glog" @@ -77,7 +78,7 @@ type Config struct { type Tail struct { Filename string Lines chan *Line - IsTailing bool + IsTailing int32 Config file *os.File @@ -108,9 +109,10 @@ func TailFile(filename string, config Config) (*Tail, error) { } t := &Tail{ - Filename: filename, - Lines: make(chan *Line), - Config: config, + Filename: filename, + Lines: make(chan *Line), + Config: config, + IsTailing: 1, } // when Logger was not specified in config, use default logger @@ -233,12 +235,10 @@ func (tail *Tail) readLine() (string, error) { } func (tail *Tail) tailFileSync() { - defer func() { tail.IsTailing = false }() + defer func() { atomic.StoreInt32(&tail.IsTailing, 0) }() defer tail.Done() defer tail.close() - tail.IsTailing = true - if !tail.MustExist { // deferred first open. err := tail.reopen() diff --git a/pkg/lobster/tailer/tailer.go b/pkg/lobster/tailer/tailer.go index f5c405f..489e147 100644 --- a/pkg/lobster/tailer/tailer.go +++ b/pkg/lobster/tailer/tailer.go @@ -21,6 +21,7 @@ import ( "log" "os" "sync" + "sync/atomic" "time" "github.com/golang/glog" @@ -183,7 +184,7 @@ func (t *Tailer) drain() { close(t.LogChan) } - for t.tail.IsTailing { + for atomic.LoadInt32(&t.tail.IsTailing) == 1 { select { case <-t.tail.Lines: default: