百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Q

queue

> DevOps
开源

QQG 软件包提供多个队列执行 。 以无线安全的一般方式发展.

362 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

QQG 软件包提供多个队列执行 。 以无线安全的一般方式发展.

queue


Thread-safe, generic FIFO, priority, circular, linked, and delay queues for Go.

Features

  • Five queue flavours behind one Queue[T comparable] interface, so you can swap implementations without changing call sites.
  • Generic types with no reflection; zero third-party dependencies.
  • Steady-state zero-alloc reads on every queue and zero-alloc offer/get on Circular, Linked, Priority, and Delay.
  • Blocking variants (OfferWait, GetWait, PeekWait) for producer/consumer workloads.
  • Delay queue for timers, retry scheduling, and TTL expiry.
  • 100% test coverage and race-tested in CI.

Full API reference at pkg.go.dev/github.com/adrianbrad/queue.

  • queue
    • Features
    • Installation
    • Import
    • Quick start
    • Choosing a queue
    • Usage
      • Queue Interface
      • Blocking Queue
      • Priority Queue
      • Circular Queue
      • Linked Queue
      • Delay Queue
    • Benchmarks
    • Contributing
    • Security
    • License

Installation

To add this package as a dependency to your project, run:

bash
go get -u github.com/adrianbrad/queue

Import

To use this package in your project, you can import it as follows:

go
import "github.com/adrianbrad/queue"

Quick start

go
package main

import (
	"fmt"

	"github.com/adrianbrad/queue"
)

func main() {
	q := queue.NewLinked([]int{1, 2, 3})

	_ = q.Offer(4)

	// prints 1, then 2, then 3, then 4 (one per iteration)
	for !q.IsEmpty() {
		v, _ := q.Get()
		fmt.Println(v)
	}
}

Every implementation satisfies the same Queue[T comparable] interface. Pick a different constructor (NewBlocking, NewPriority, NewCircular, NewDelay) without changing the call sites.

Choosing a queue

Queue Ordering Capacity Blocks? Pick this when…
Blocking FIFO Optional; Offer errors on full Yes, via OfferWait, GetWait, PeekWait You want a classic producer-consumer queue with backpressure and blocking semantics.
Priority Custom (less func) Optional; Offer errors on full No Order depends on a computed value (smallest deadline, highest score, lexicographic, etc).
Circular FIFO Required; Offer overwrites the oldest No You want fixed memory and the most recent N items; dropping older entries is acceptable.
Linked FIFO None (unbounded) No You need an unbounded FIFO and don't want to pick a capacity up front.
Delay By deadline Optional; Offer errors on full GetWait sleeps until the head's deadline passes Items should become available at a future time (timers, retry scheduling, TTL expiry).

Usage

Queue Interface

…

Blocking Queue

Blocking queue is a FIFO ordered data structure. Both blocking and non-blocking methods are implemented. Blocking methods wait for the queue to have available items when dequeuing, and wait for a slot to become available in case the queue is full when enqueuing. The non-blocking methods return an error if an element cannot be added or removed. Implemented using sync.Cond from the standard library.

go
package main

import (
	"fmt"

	"github.com/adrianbrad/queue"
)

func main() {
	elems := []int{2, 3}

	blockingQueue := queue.NewBlocking(elems, queue.WithCapacity(3))

	containsTwo := blockingQueue.Contains(2)
	fmt.Println(containsTwo) // true

	size := blockingQueue.Size()
	fmt.Println(size) // 2

	empty := blockingQueue.IsEmpty()
	fmt.Println(empty) // false

	if err := blockingQueue.Offer(1); err != nil {
		// handle err
	}

	elem, err := blockingQueue.Get()
	if err != nil {
		// handle err
	}

	fmt.Printf("elem: %d\n", elem) // elem: 2
}

Priority Queue

Priority Queue is a data structure where the order of the elements is given by a less function provided at construction. Implemented over an internal min-heap.

…

Circular Queue

Circular Queue is a fixed size FIFO ordered data structure. When the queue is full, adding a new element to the queue overwrites the oldest element.

Example: We have the following queue with a capacity of 3 elements: [1, 2, 3]. If the tail of the queue is set to 0, as if we just added the element 3, the next element to be added to the queue will overwrite the element at index 0. So, if we add the element 4, the queue will look like this: [4, 2, 3]. If the head of the queue is set to 0, as if we never removed an element yet, then the next element to be removed from the queue will be the element at index 0, which is 4.

go
package main

import (
	"fmt"

	"github.com/adrianbrad/queue"
)

func main() {
	elems := []int{2, 3, 4}

	circularQueue := queue.NewCircular(elems, 3)

	containsTwo := circularQueue.Contains(2)
	fmt.Println(containsTwo) // true

	size := circularQueue.Size()
	fmt.Println(size) // 3

	empty := circularQueue.IsEmpty()
	fmt.Println(empty) // false

	if err := circularQueue.Offer(1); err != nil {
		// handle err
	}

	elem, err := circularQueue.Get()
	if err != nil {
		// handle err
	}

	fmt.Printf("elem: %d\n", elem) // elem: 1
}

Linked Queue

A linked queue, implemented as a singly linked list, offering O(1) time complexity for enqueue and dequeue operations. The queue maintains pointers to both the head (front) and tail (end) of the list for efficient operations without the need for traversal.

go
package main

import (
	"fmt"

	"github.com/adrianbrad/queue"
)

func main() {
	elems := []int{2, 3, 4}

	linkedQueue := queue.NewLinked(elems)

	containsTwo := linkedQueue.Contains(2)
	fmt.Println(containsTwo) // true

	size := linkedQueue.Size()
	fmt.Println(size) // 3

	empty := linkedQueue.IsEmpty()
	fmt.Println(empty) // false

	if err := linkedQueue.Offer(1); err != nil {
		// handle err
	}

	elem, err := linkedQueue.Get()
	if err != nil {
		// handle err
	}

	fmt.Printf("elem: %d\n", elem) // elem: 2
}

Delay Queue

A Delay queue is a priority queue where each element becomes dequeuable at a deadline computed by a caller-supplied function at Offer time. Get returns ErrNoElementsAvailable until the head's deadline has passed; GetWait sleeps until it does. Useful for timers, retry scheduling, and TTL expiry.

…

Benchmarks

Run locally with go test -bench=. -benchmem -benchtime=3s -count=3. Reported numbers are per-operation timings and allocations; absolute values vary by hardware, but the shape (zero-alloc reads everywhere, zero-alloc offer/get for Circular, Linked, Priority, and Delay) should be stable.

…

Contributing

PRs welcome. See CONTRIBUTING.md for the workflow, coding conventions, and the 100% coverage requirement. Ask questions by opening a GitHub issue.

Security

Please report security issues privately following SECURITY.md rather than opening a public issue.

License

MIT. See LICENSE. Maintained by @adrianbrad.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Goblocking-queuecircular-queuedelay-queuegeneric

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类DevOps
定价开源

> 相关工具

D
Docker
容器化平台,标准化应用交付
G
GitHub Actions
GitHub 原生 CI/CD 工作流
N
Nginx
高性能 Web 服务器与反向代理