mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
gqueue新增Close方法便于队列优雅退出
This commit is contained in:
@ -48,6 +48,11 @@ func (q *IntQueue) Pop() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 关闭队列(通知所有通过Pop阻塞的协程退出)
|
||||
func (q *IntQueue) Close() {
|
||||
q.events <- struct{}{}
|
||||
}
|
||||
|
||||
// 获取当前队列大小
|
||||
func (q *IntQueue) Size() int {
|
||||
return len(q.events)
|
||||
|
||||
@ -49,6 +49,11 @@ func (q *InterfaceQueue) Pop() interface{} {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 关闭队列(通知所有通过Pop阻塞的协程退出)
|
||||
func (q *InterfaceQueue) Close() {
|
||||
q.events <- struct{}{}
|
||||
}
|
||||
|
||||
// 获取当前队列大小
|
||||
func (q *InterfaceQueue) Size() int {
|
||||
return len(q.events)
|
||||
|
||||
@ -48,6 +48,11 @@ func (q *StringQueue) Pop() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// 关闭队列(通知所有通过Pop阻塞的协程退出)
|
||||
func (q *StringQueue) Close() {
|
||||
q.events <- struct{}{}
|
||||
}
|
||||
|
||||
// 获取当前队列大小
|
||||
func (q *StringQueue) Size() int {
|
||||
return len(q.events)
|
||||
|
||||
@ -48,6 +48,11 @@ func (q *UintQueue) Pop() uint {
|
||||
return 0
|
||||
}
|
||||
|
||||
// 关闭队列(通知所有通过Pop阻塞的协程退出)
|
||||
func (q *UintQueue) Close() {
|
||||
q.events <- struct{}{}
|
||||
}
|
||||
|
||||
// 获取当前队列大小
|
||||
func (q *UintQueue) Size() int {
|
||||
return len(q.events)
|
||||
|
||||
@ -1,8 +1,36 @@
|
||||
package main
|
||||
|
||||
import "gitee.com/johng/gf/g/encoding/gparser"
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
gparser.Load("/home/john/Workspace/Go/GOPATH/src/gitee.com/johng/gf/geg/frame/config.yml")
|
||||
events1 := make(chan int, 100)
|
||||
events2 := make(chan int, 100)
|
||||
go func() {
|
||||
for{
|
||||
select {
|
||||
case t1 := <-events1:
|
||||
fmt.Println(t1)
|
||||
case t2 := <-events2:
|
||||
fmt.Println(t2)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}()
|
||||
|
||||
go func() {
|
||||
time.Sleep(2*time.Second)
|
||||
events1 <- 1
|
||||
events2 <- 2
|
||||
time.Sleep(2*time.Second)
|
||||
close(events1)
|
||||
close(events2)
|
||||
}()
|
||||
|
||||
select {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user