Skip to content

Instantly share code, notes, and snippets.

@horus
Last active December 18, 2023 02:31
Show Gist options
  • Select an option

  • Save horus/908c41c8c9fca7e3fd872dfa053db399 to your computer and use it in GitHub Desktop.

Select an option

Save horus/908c41c8c9fca7e3fd872dfa053db399 to your computer and use it in GitHub Desktop.
fix: send on closed channel
type printer struct {
sync.Mutex
queue chan api.ContainerEvent
consumer api.LogConsumer
stopped bool
}
// newLogPrinter builds a LogPrinter passing containers logs to LogConsumer
func newLogPrinter(consumer api.LogConsumer) logPrinter {
queue := make(chan api.ContainerEvent)
printer := printer{
consumer: consumer,
queue: queue,
}
return &printer
}
func (p *printer) Stop() {
p.Lock()
defer p.Unlock()
if !p.stopped {
p.stopped = true
close(p.queue)
}
}
func (p *printer) HandleEvent(event api.ContainerEvent) {
// prevent deadlocking, if the printer is done, there's no reader for
// queue, so this write could block indefinitely
p.Lock()
defer p.Unlock()
if p.stopped {
return
}
p.queue <- event
}
//nolint:gocyclo
func (p *printer) Run(cascadeStop bool, exitCodeFrom string, stopFn func() error) (int, error) {
var (
aborting bool
exitCode int
)
... ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment