Merge branch 'gogf:master' into zhiwei

This commit is contained in:
564104865 2021-11-10 16:37:56 +08:00 committed by GitHub
commit 4862505c5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 11 deletions

View File

@ -59,7 +59,7 @@ func New(limit ...int) *Queue {
}
// asyncLoopFromListToChannel starts an asynchronous goroutine,
// which handles the data synchronization from list <q.list> to channel <q.C>.
// which handles the data synchronization from list `q.list` to channel `q.C`.
func (q *Queue) asyncLoopFromListToChannel() {
defer func() {
if q.closed.Val() {
@ -87,13 +87,13 @@ func (q *Queue) asyncLoopFromListToChannel() {
<-q.events
}
}
// It should be here to close q.C if `q` is unlimited size.
// It should be here to close `q.C` if `q` is unlimited size.
// It's the sender's responsibility to close channel when it should be closed.
close(q.C)
}
// Push pushes the data `v` into the queue.
// Note that it would panics if Push is called after the queue is closed.
// Note that it would panic if Push is called after the queue is closed.
func (q *Queue) Push(v interface{}) {
if q.limit > 0 {
q.C <- v
@ -121,14 +121,15 @@ func (q *Queue) Close() {
}
if q.limit > 0 {
close(q.C)
}
for i := 0; i < defaultBatchSize; i++ {
q.Pop()
} else {
for i := 0; i < defaultBatchSize; i++ {
q.Pop()
}
}
}
// Len returns the length of the queue.
// Note that the result might not be accurate as there's a
// Note that the result might not be accurate as there's an
// asynchronous channel reading the list constantly.
func (q *Queue) Len() (length int) {
if q.list != nil {

View File

@ -65,10 +65,14 @@ func ExampleQueue_Push() {
q.Push(i)
}
fmt.Println(q.Len())
fmt.Println(q.Pop())
fmt.Println(q.Pop())
fmt.Println(q.Pop())
// Output:
// 10
// 0
// 1
// 2
}
func ExampleQueue_Pop() {
@ -113,7 +117,7 @@ func ExampleQueue_Len() {
fmt.Println(q.Len())
// Output:
// May Output:
// 2
}
@ -126,6 +130,6 @@ func ExampleQueue_Size() {
// Size is alias of Len.
fmt.Println(q.Size())
// Output:
// May Output:
// 2
}