-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexample_test.go
More file actions
47 lines (41 loc) · 937 Bytes
/
Copy pathexample_test.go
File metadata and controls
47 lines (41 loc) · 937 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
43
44
45
46
47
package batching_test
import (
"fmt"
"time"
"code.cloudfoundry.org/go-batching"
)
func ExampleByteBatcher() {
writer := batching.ByteWriterFunc(func(batch [][]byte) {
for _, data := range batch {
fmt.Printf("%s\n", data)
}
})
batcher := batching.NewByteBatcher(100, time.Nanosecond, writer)
dataSource := make(chan []byte)
done := make(chan struct{})
go func() {
defer close(done)
for i := 0; i < 3; i++ {
dataSource <- []byte(fmt.Sprintf("data %d", i))
}
}()
for {
// Do a non-blocking read from a data source.
select {
case data := <-dataSource:
// If read succeeds write it out. This will flush if the batch
// exceeds the batch size.
batcher.Write(data)
case <-done:
return
default:
// If read fails make sure to call Flush to ensure data doesn't
// get stuck in the batch for long periods of time.
batcher.Flush()
}
}
// Output:
// data 0
// data 1
// data 2
}