queue

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package queue 提供固定容量、并发安全的泛型队列。

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidCapacity 表示队列容量小于等于零。
	ErrInvalidCapacity = errors.New("queue: invalid capacity")
	// ErrFull 表示队列当前没有可用槽位。
	ErrFull = errors.New("queue: full")
	// ErrClosed 表示队列已经关闭。
	ErrClosed = errors.New("queue: closed")
	// ErrNilContext 表示阻塞入队缺少 Context。
	ErrNilContext = errors.New("queue: nil context")
)

Functions

This section is empty.

Types

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue 是固定容量、多生产者、单消费者的环形队列。

func New

func New[T any](capacity int) (*Queue[T], error)

New 创建一个指定容量的有界队列。

func (*Queue[T]) Capacity

func (q *Queue[T]) Capacity() int

Capacity 返回队列总容量。

func (*Queue[T]) Close

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

Close 关闭队列;已经入队的元素仍可被取出。

func (*Queue[T]) Closed

func (q *Queue[T]) Closed() bool

Closed 返回队列是否已经关闭。

func (*Queue[T]) Depth

func (q *Queue[T]) Depth() int

Depth 返回当前队列深度。

func (*Queue[T]) Pop

func (q *Queue[T]) Pop() (T, bool)

Pop 取出一条最早入队的元素。

func (*Queue[T]) PopBatch

func (q *Queue[T]) PopBatch(max int) ([]T, bool)

PopBatch 最多批量取出 max 条元素,并保持 FIFO 顺序。

func (*Queue[T]) Push

func (q *Queue[T]) Push(ctx context.Context, value T) error

Push 在队列满时等待空间,并响应 Context 取消或队列关闭。

func (*Queue[T]) TryPush

func (q *Queue[T]) TryPush(value T) error

TryPush 尝试立即入队,队列满时返回 ErrFull。

Jump to

Keyboard shortcuts

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