infra
/
goutils
Archived
1
0
Fork 0
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.
goutils/async/iface_queue.go

37 lines
684 B
Go

package async
import (
"container/list"
)
// 블럭되지 않는 큐체널
func NewQueue() (chan<- interface{}, <-chan interface{}) {
send := make(chan interface{}, 1)
receive := make(chan interface{}, 1)
go manageQueue(send, receive)
return send, receive
}
func manageQueue(send <-chan interface{}, receive chan<- interface{}) {
queue := list.New()
defer close(receive)
for {
if front := queue.Front(); front == nil {
if value, ok := <-send; ok {
queue.PushBack(value)
} else {
break
}
} else {
select {
case receive <- front.Value:
queue.Remove(front)
case value, ok := <-send:
if ok {
queue.PushBack(value)
}
}
}
}
}