This repository has been archived on 2022-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
2018-06-06 15:30:23 +09:00
|
|
|
package buf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
|
|
|
)
|
|
|
|
|
2018-06-09 12:31:21 +09:00
|
|
|
// 블럭되지 않는 큐체널
|
2018-06-06 15:30:23 +09:00
|
|
|
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()
|
2018-06-09 12:31:21 +09:00
|
|
|
defer close(receive)
|
2018-06-06 15:30:23 +09:00
|
|
|
for {
|
|
|
|
if front := queue.Front(); front == nil {
|
2018-06-09 12:31:21 +09:00
|
|
|
if value, ok := <-send; ok {
|
|
|
|
queue.PushBack(value)
|
|
|
|
} else {
|
|
|
|
break
|
2018-06-06 15:30:23 +09:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
select {
|
|
|
|
case receive <- front.Value.([]byte):
|
|
|
|
queue.Remove(front)
|
|
|
|
case value, ok := <-send:
|
|
|
|
if ok {
|
|
|
|
queue.PushBack(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|