diff --git a/buf/bytes_queue.go b/async/bytes_queue.go similarity index 97% rename from buf/bytes_queue.go rename to async/bytes_queue.go index 0dd5cf8..a86bbe9 100644 --- a/buf/bytes_queue.go +++ b/async/bytes_queue.go @@ -1,4 +1,4 @@ -package buf +package async import ( "container/list" diff --git a/async/iface_queue.go b/async/iface_queue.go new file mode 100644 index 0000000..fdc835e --- /dev/null +++ b/async/iface_queue.go @@ -0,0 +1,36 @@ +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.(interface{}): + queue.Remove(front) + case value, ok := <-send: + if ok { + queue.PushBack(value) + } + } + } + } +} diff --git a/buf/string_queue.go b/async/string_queue.go similarity index 97% rename from buf/string_queue.go rename to async/string_queue.go index 1be0f61..ca16710 100644 --- a/buf/string_queue.go +++ b/async/string_queue.go @@ -1,4 +1,4 @@ -package buf +package async import ( "container/list"