Skip to content

Commit

Permalink
fix: fixes case where container termination may stop log tailing too …
Browse files Browse the repository at this point in the history
…early.
  • Loading branch information
atombender committed Oct 26, 2022
1 parent 80ddf41 commit b49c545
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"strings"
"sync/atomic"
"time"

"github.com/jpillora/backoff"
Expand Down Expand Up @@ -54,7 +55,7 @@ type ContainerTailer struct {
client kubernetes.Interface
pod v1.Pod
container v1.Container
stop bool
stop atomic.Bool
eventFunc LogEventFunc
fromTimestamp *time.Time
errorBackoff *backoff.Backoff
Expand All @@ -63,12 +64,12 @@ type ContainerTailer struct {
}

func (ct *ContainerTailer) Stop() {
ct.stop = true
ct.stop.Store(true)
}

func (ct *ContainerTailer) Run(ctx context.Context, onError func(err error)) {
ct.errorBackoff.Reset()
for !ct.stop {
for !ct.stop.Load() {
stream, err := ct.getStream(ctx)
if err != nil {
time.Sleep(ct.errorBackoff.Duration())
Expand All @@ -92,13 +93,13 @@ func (ct *ContainerTailer) runStream(stream io.ReadCloser) error {
}()

r := bufio.NewReader(stream)
for !ct.stop {
for {
line, err := r.ReadString('\n')
if err == io.EOF {
break
}
if err != nil {
if err != io.EOF {
return err
}
return nil
return err
}
ct.errorBackoff.Reset()
ct.receiveLine(line)
Expand Down

0 comments on commit b49c545

Please sign in to comment.