From b15525f489c5f0906d96fc4c54d51ab64c23301c Mon Sep 17 00:00:00 2001 From: Sangbum Kim Date: Sat, 9 Jun 2018 12:58:26 +0900 Subject: [PATCH] =?UTF-8?q?=ED=8C=A8=ED=82=A4=EC=A7=80=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- {buf => async}/bytes_queue.go | 2 +- async/iface_queue.go | 36 ++++++++++++++++++++++++++++++++++ {buf => async}/string_queue.go | 2 +- 3 files changed, 38 insertions(+), 2 deletions(-) rename {buf => async}/bytes_queue.go (97%) create mode 100644 async/iface_queue.go rename {buf => async}/string_queue.go (97%) 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"