forked from abdullin/cellar
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreader_async.go
More file actions
42 lines (36 loc) · 886 Bytes
/
reader_async.go
File metadata and controls
42 lines (36 loc) · 886 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package cellar
import (
"context"
)
type Rec struct {
Data []byte
ChunkPos int64
StartPos int64
NextPos int64
}
// ScanAsync runs Reader.Scan in a goroutine, returning the values obtained.
//
// ScanAsync honors context cancellations. If an error is received in the error channel, no more values will
// be scanned and the routine exits.
func (reader *Reader) ScanAsync(ctx context.Context, buffer int) (chan Rec, chan error) {
vals := make(chan Rec, buffer)
errs := make(chan error)
go func() {
// make sure we terminate the channel on scan read
defer close(vals)
defer close(errs)
err := reader.Scan(func(ri *ReaderInfo, data []byte) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
vals <- Rec{data, ri.ChunkPos, ri.StartPos, ri.NextPos}
return nil
}
})
if err != nil {
errs <- err
}
}()
return vals, errs
}