forked from timescale/tsbs
-
Notifications
You must be signed in to change notification settings - Fork 2
/
duplex_channel.go
36 lines (30 loc) · 1.09 KB
/
duplex_channel.go
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
package load
import "github.com/benchant/tsbs/pkg/targets"
// duplexChannel acts as a two-way channel for communicating from a scan routine
// to a worker goroutine. The toWorker channel sends data to the worker for it
// to process and the toScan channel allows the worker to acknowledge completion.
// Using this we can accomplish better flow control between the scanner and workers.
type duplexChannel struct {
toWorker chan targets.Batch
toScanner chan bool
}
// newDuplexChannel returns a duplexChannel with specified buffer sizes
func newDuplexChannel(queueLen int) *duplexChannel {
return &duplexChannel{
toWorker: make(chan targets.Batch, queueLen),
toScanner: make(chan bool, queueLen),
}
}
// sendToWorker passes a batch of work on to the worker from the scanner
func (dc *duplexChannel) sendToWorker(b targets.Batch) {
dc.toWorker <- b
}
// sendToScanner passes an acknowledge to the scanner from the worker
func (dc *duplexChannel) sendToScanner() {
dc.toScanner <- true
}
// close closes down the duplexChannel
func (dc *duplexChannel) close() {
close(dc.toWorker)
close(dc.toScanner)
}