gqueue

package
v2.10.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 3 Imported by: 27

Documentation

Overview

Package gqueue provides dynamic/static concurrent-safe queue.

Features:

1. FIFO queue(data -> list -> chan);

2. Fast creation and initialization;

3. Support dynamic queue size(unlimited queue size);

4. Blocking when reading data from queue;

Copyright GoFrame Author(https://goframe.org). All Rights Reserved.

This Source Code Form is subject to the terms of the MIT License. If a copy of the MIT was not distributed with this file, You can obtain one at https://github.com/gogf/gf.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Queue

type Queue struct {
	*TQueue[any]
}

Queue is a concurrent-safe queue built on doubly linked list and channel.

func New

func New(limit ...int) *Queue

New returns an empty queue object. Optional parameter `limit` is used to limit the size of the queue, which is unlimited in default. When `limit` is given, the queue will be static and high performance which is comparable with stdlib channel.

Example
package main

import (
	"context"
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gqueue"
	"github.com/gogf/gf/v2/os/gtimer"
)

func main() {
	n := 10
	q := gqueue.New()

	// Producer
	for i := 0; i < n; i++ {
		q.Push(i)
	}

	// Close the queue in three seconds.
	gtimer.SetTimeout(context.Background(), time.Second*3, func(ctx context.Context) {
		q.Close()
	})

	// The consumer constantly reads the queue data.
	// If there is no data in the queue, it will block.
	// The queue is read using the queue.C property exposed
	// by the queue object and the selectIO multiplexing syntax
	// example:
	// for {
	//    select {
	//        case v := <-queue.C:
	//            if v != nil {
	//                fmt.Println(v)
	//            } else {
	//                return
	//            }
	//    }
	// }
	for {
		if v := q.Pop(); v != nil {
			fmt.Print(v)
		} else {
			break
		}
	}

}
Output:

0123456789

func (*Queue) Close

func (q *Queue) Close()

Close closes the queue. Notice: It would notify all goroutines return immediately, which are being blocked reading using Pop method.

Example
package main

import (
	"fmt"
	"time"

	"github.com/gogf/gf/v2/container/gqueue"
)

func main() {
	q := gqueue.New()

	for i := 0; i < 10; i++ {
		q.Push(i)
	}

	time.Sleep(time.Millisecond)
	q.Close()

	fmt.Println(q.Len())
	fmt.Println(q.Pop())

	// May Output:
	// 0
	// <nil>
}

func (*Queue) Len

func (q *Queue) Len() (length int64)

Len returns the length of the queue. Note that the result might not be accurate if using unlimited queue size as there's an asynchronous channel reading the list constantly.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gqueue"
)

func main() {
	q := gqueue.New()

	q.Push(1)
	q.Push(2)

	fmt.Println(q.Len())

	// May Output:
	// 2
}

func (*Queue) Pop

func (q *Queue) Pop() any

Pop pops an item from the queue in FIFO way. Note that it would return nil immediately if Pop is called after the queue is closed.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gqueue"
)

func main() {
	q := gqueue.New()

	for i := 0; i < 10; i++ {
		q.Push(i)
	}

	fmt.Println(q.Pop())
	q.Close()
	fmt.Println(q.Pop())

}
Output:

0
<nil>

func (*Queue) Push

func (q *Queue) Push(v any)

Push pushes the data `v` into the queue. Note that it would panic if Push is called after the queue is closed.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gqueue"
)

func main() {
	q := gqueue.New()

	for i := 0; i < 10; i++ {
		q.Push(i)
	}

	fmt.Println(q.Pop())
	fmt.Println(q.Pop())
	fmt.Println(q.Pop())

}
Output:

0
1
2

func (*Queue) Size deprecated

func (q *Queue) Size() int64

Size is alias of Len.

Deprecated: use Len instead.

Example
package main

import (
	"fmt"

	"github.com/gogf/gf/v2/container/gqueue"
)

func main() {
	q := gqueue.New()

	q.Push(1)
	q.Push(2)

	// Size is alias of Len.
	fmt.Println(q.Size())

	// May Output:
	// 2
}

type TQueue added in v2.9.6

type TQueue[T any] struct {
	C chan T // Underlying channel for data reading.
	// contains filtered or unexported fields
}

TQueue is a concurrent-safe queue built on doubly linked list and channel.

func NewTQueue added in v2.9.6

func NewTQueue[T any](limit ...int) *TQueue[T]

NewTQueue returns an empty queue object. Optional parameter `limit` is used to limit the size of the queue, which is unlimited in default. When `limit` is given, the queue will be static and high performance which is comparable with stdlib channel.

func (*TQueue[T]) Close added in v2.9.6

func (q *TQueue[T]) Close()

Close closes the queue. Notice: It would notify all goroutines return immediately, which are being blocked reading using Pop method.

func (*TQueue[T]) Len added in v2.9.6

func (q *TQueue[T]) Len() (length int64)

Len returns the length of the queue. Note that the result might not be accurate if using unlimited queue size as there's an asynchronous channel reading the list constantly.

func (*TQueue[T]) Pop added in v2.9.6

func (q *TQueue[T]) Pop() T

Pop pops an item from the queue in FIFO way. Note that it would return nil immediately if Pop is called after the queue is closed.

func (*TQueue[T]) Push added in v2.9.6

func (q *TQueue[T]) Push(v T)

Push pushes the data `v` into the queue. Note that it would panic if Push is called after the queue is closed.

func (*TQueue[T]) Size deprecated added in v2.9.6

func (q *TQueue[T]) Size() int64

Size is alias of Len.

Deprecated: use Len instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL