package main import ( "context" "fmt" ) type LoaderError struct { Chunk Msg string } func (e *LoaderError) Error() string { return e.Msg } func loaderErrorHandler( ctx context.Context, chErrorsIn <-chan LoaderError, chChunksOut chan<- Chunk, chJobErrorsOut chan<- JobError, ) { for { if ctx.Err() != nil { return } select { case <-ctx.Done(): return case err, ok := <-chErrorsIn: if !ok { return } if err.RetryCounter >= maxRetryAttempts { jobError := JobError{ ShouldCancelJob: false, Msg: fmt.Sprintf("chunk %v reached max retries (%d)", err.Id, maxRetryAttempts), Prev: &err, } select { case chJobErrorsOut <- jobError: case <-ctx.Done(): return } continue } err.RetryCounter++ select { case chChunksOut <- err.Chunk: case <-ctx.Done(): return } } } }