Skip to content

Commit

Permalink
feat: cleanup goroutines management (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
derdanne authored Feb 3, 2023
1 parent 9a812a2 commit aed94a4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
22 changes: 12 additions & 10 deletions stern/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ OUTER:
}
tailsMutex.Lock()
tails[id].Close()
tails[id].PrintClose()
delete(tails, id)
tailsMutex.Unlock()
}
Expand All @@ -204,17 +205,18 @@ OUTER:
}
}()

go func() {
OUTER:
for {
select {
case str := <-logC:
fmt.Fprint(os.Stdout, str)
case <-ctx.Done():
break OUTER
if config.GraylogServer == "" {
go func() {
for {
select {
case str := <-logC:
fmt.Fprint(os.Stdout, str)
case <-ctx.Done():
return
}
}
}
}()
}()
}

go func() {
defer w.Done()
Expand Down
9 changes: 2 additions & 7 deletions stern/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,14 +225,15 @@ func (t *Tail) Start(ctx context.Context, i v1.PodInterface, gelfWriter *gelf.TC
t.Active = false
return
}
t.PrintOpen()
defer stream.Close()
t.PrintOpen()

go func() {
<-t.closed
stream.Close()
t.Active = false
}()

reader := bufio.NewReader(stream)

OUTER:
Expand Down Expand Up @@ -296,7 +297,6 @@ func (t *Tail) Start(ctx context.Context, i v1.PodInterface, gelfWriter *gelf.TC
}
}
} else {

vm := Log{
Message: logLine,
Namespace: t.Namespace,
Expand All @@ -318,11 +318,6 @@ func (t *Tail) Start(ctx context.Context, i v1.PodInterface, gelfWriter *gelf.TC
}
}
}()

go func() {
<-ctx.Done()
close(t.closed)
}()
}

// Close stops tailing
Expand Down

0 comments on commit aed94a4

Please sign in to comment.