2022-08-10 17:11:13 +00:00
|
|
|
package sources
|
2022-02-04 20:14:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
type ChunkFunc func(chunk *Chunk) error
|
2022-02-04 20:14:42 +00:00
|
|
|
|
2022-02-16 01:38:19 +00:00
|
|
|
var MatchError = errors.New("chunk doesn't match")
|
2022-02-04 20:14:42 +00:00
|
|
|
|
2022-08-10 17:11:13 +00:00
|
|
|
func HandleTestChannel(chunksCh chan *Chunk, cf ChunkFunc) error {
|
2022-02-04 20:14:42 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case gotChunk := <-chunksCh:
|
|
|
|
err := cf(gotChunk)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, MatchError) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
case <-time.After(10 * time.Second):
|
2023-01-25 16:57:39 +00:00
|
|
|
return fmt.Errorf("no new chunks received after 10 seconds")
|
2022-02-04 20:14:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|