Last active
December 18, 2023 02:31
-
-
Save horus/908c41c8c9fca7e3fd872dfa053db399 to your computer and use it in GitHub Desktop.
fix: send on closed channel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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