Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix variable to use atomic #25

Merged
merged 1 commit into from
Dec 13, 2024
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
14 changes: 7 additions & 7 deletions pkg/lobster/tailer/tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"strings"
"sync"
"sync/atomic"
"time"

"github.com/golang/glog"
Expand Down Expand Up @@ -77,7 +78,7 @@ type Config struct {
type Tail struct {
Filename string
Lines chan *Line
IsTailing bool
IsTailing int32
Config

file *os.File
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion pkg/lobster/tailer/tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"os"
"sync"
"sync/atomic"
"time"

"github.com/golang/glog"
Expand Down Expand Up @@ -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:
Expand Down
Loading