43 lines
751 B
Go
43 lines
751 B
Go
|
package buf
|
||
|
|
||
|
import (
|
||
|
"container/list"
|
||
|
)
|
||
|
|
||
|
// 블럭되지 않는 큐체널
|
||
|
func NewStringQueue() (chan<- string, <-chan string) {
|
||
|
send := make(chan string, 1)
|
||
|
receive := make(chan string, 1)
|
||
|
go manageStringQueue(send, receive)
|
||
|
return send, receive
|
||
|
}
|
||
|
|
||
|
func manageStringQueue(send <-chan string, receive chan<- string) {
|
||
|
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.(string):
|
||
|
queue.Remove(front)
|
||
|
case value, ok := <-send:
|
||
|
if ok {
|
||
|
queue.PushBack(value)
|
||
|
} else {
|
||
|
send = nil
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|