Use a ring channel to avoid blocking write of events (#2082)

* Use a ring channel to avoid blocking write of events

* Add eapache/channels dependency
This commit is contained in:
Manuel Alejandro de Brito Fontes 2018-02-13 17:46:18 -08:00 committed by GitHub
parent 33475b7184
commit 9bcb5b08ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
35 changed files with 2833 additions and 78 deletions

54
vendor/github.com/eapache/channels/black_hole.go generated vendored Normal file
View file

@ -0,0 +1,54 @@
package channels
// BlackHole implements the InChannel interface and provides an analogue for the "Discard" variable in
// the ioutil package - it never blocks, and simply discards every value it reads. The number of items
// discarded in this way is counted and returned from Len.
type BlackHole struct {
input chan interface{}
length chan int
count int
}
func NewBlackHole() *BlackHole {
ch := &BlackHole{
input: make(chan interface{}),
length: make(chan int),
}
go ch.discard()
return ch
}
func (ch *BlackHole) In() chan<- interface{} {
return ch.input
}
func (ch *BlackHole) Len() int {
val, open := <-ch.length
if open {
return val
} else {
return ch.count
}
}
func (ch *BlackHole) Cap() BufferCap {
return Infinity
}
func (ch *BlackHole) Close() {
close(ch.input)
}
func (ch *BlackHole) discard() {
for {
select {
case _, open := <-ch.input:
if !open {
close(ch.length)
return
}
ch.count++
case ch.length <- ch.count:
}
}
}