42 lines
715 B
Go
42 lines
715 B
Go
|
package buf
|
||
|
|
||
|
import (
|
||
|
"container/list"
|
||
|
)
|
||
|
|
||
|
func NewBytesQueue() (chan<- []byte, <-chan []byte) {
|
||
|
send := make(chan []byte, 1)
|
||
|
receive := make(chan []byte, 1)
|
||
|
go manageBytesQueue(send, receive)
|
||
|
return send, receive
|
||
|
}
|
||
|
|
||
|
func manageBytesQueue(send <-chan []byte, receive chan<- []byte) {
|
||
|
queue := list.New()
|
||
|
for {
|
||
|
if front := queue.Front(); front == nil {
|
||
|
if send == nil {
|
||
|
close(receive)
|
||
|
return
|
||
|
}
|
||
|
value, ok := <-send
|
||
|
if !ok {
|
||
|
close(receive)
|
||
|
return
|
||
|
}
|
||
|
queue.PushBack(value)
|
||
|
} else {
|
||
|
select {
|
||
|
case receive <- front.Value.([]byte):
|
||
|
queue.Remove(front)
|
||
|
case value, ok := <-send:
|
||
|
if ok {
|
||
|
queue.PushBack(value)
|
||
|
} else {
|
||
|
send = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|