Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ name: Test

on:
push:
branches: [ main, master ]
tags:
- 'v*'
branches: [ develop ]
pull_request:
branches: [ master ]
branches: [ develop ]

jobs:
test:
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
.tmp*
89 changes: 35 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
> [!NOTE]
> This README was generated by [SKILL](https://github.com/pardnchiu/skill-readme-generate), get the ZH version from [here](./README.zh.md).
> This README was generated by [SKILL](https://github.com/agenvoy/skill-readme-generate), get the ZH version from [here](./doc/README.zh.md).

# go-queue
***

<p align="center">
<strong>PRIORITY-AWARE TASK QUEUE FOR GO</strong>
</p>

[![pkg](https://pkg.go.dev/badge/github.com/pardnchiu/go-queue.svg)](https://pkg.go.dev/github.com/pardnchiu/go-queue)
[![card](https://goreportcard.com/badge/github.com/pardnchiu/go-queue)](https://goreportcard.com/report/github.com/pardnchiu/go-queue)
[![codecov](https://img.shields.io/codecov/c/github/pardnchiu/go-queue/master)](https://app.codecov.io/github/pardnchiu/go-queue/tree/master)
[![license](https://img.shields.io/github/license/pardnchiu/go-queue)](LICENSE)
[![version](https://img.shields.io/github/v/tag/pardnchiu/go-queue?label=release)](https://github.com/pardnchiu/go-queue/releases)
<p align="center">
<a href="https://pkg.go.dev/github.com/pardnchiu/go-queue"><img src="https://img.shields.io/badge/GO-REFERENCE-blue?include_prereleases&style=for-the-badge" alt="Go Reference"></a>
<a href="https://github.com/pardnchiu/go-queue/releases"><img src="https://img.shields.io/github/v/tag/pardnchiu/go-queue?include_prereleases&style=for-the-badge" alt="Release"></a>
<a href="LICENSE"><img src="https://img.shields.io/github/license/pardnchiu/go-queue?include_prereleases&style=for-the-badge" alt="License"></a>
<a href="https://app.codecov.io/github/pardnchiu/go-queue/tree/develop"><img src="https://img.shields.io/codecov/c/github/pardnchiu/go-queue/develop?include_prereleases&style=for-the-badge" alt="Coverage"></a>
</p>

***

> A min-heap based priority task queue with automatic promotion, retry, and panic recovery, designed for Go concurrency workloads.
> A Go task queue library with priority scheduling, timeout retries, and an atomic state machine

## Table of Contents

- [Features](#features)
- [Architecture](#architecture)
- [File Structure](#file-structure)
- [License](#license)
- [Author](#author)
- [Stars](#stars)

## Features

> `go get github.com/pardnchiu/go-queue` · [Documentation](./doc.md)

### Five-Level Priority Scheduling with Auto-Promotion

Tasks are scheduled through a min-heap across five priority levels: Immediate, High, Retry, Normal, and Low. Low-priority tasks automatically promote to higher levels after exceeding a configurable wait threshold, eliminating starvation without manual intervention.

### Atomic State Machine Lifecycle

Queue state transitions from Created to Running to Closed are driven entirely by `atomic.CompareAndSwap`, avoiding mutexes on the Start and Shutdown paths. This ensures safe concurrent access from multiple goroutines with minimal latency.
> `go get github.com/pardnchiu/go-queue` · [Documentation](./doc/doc.md)

### Built-in Retry with Panic Recovery

Each task supports a configurable retry limit; failed tasks re-enter the heap at Retry priority. Workers automatically recover from panics and convert them to error reports, preventing a single task crash from taking down the entire worker pool.
- **Five-Level Priority Queue** — Orders Immediate, High, Retry, Normal, and Low tasks, with automatic promotion after wait timeouts.
- **Worker Pool Parallelism** — Defaults to CPU×2 workers with configurable queue capacity and global timeout.
- **Timeout and Retry** — Isolates panics, enforces per-task timeouts, and re-enqueues failed tasks at the highest priority.
- **Functional Options** — Configures each enqueue with WithTaskID, WithTimeout, WithCallback, and WithRetry.
- **Atomic State Machine** — Transitions Created, Running, and Closed via CAS for lock-free, idempotent Shutdown.

## Architecture

> [Full Architecture](./doc/architecture.md)

```mermaid
graph TB
E[Enqueue] -->|Push| H[Min-Heap]
H -->|Pop| W[Worker Pool]
W -->|Execute| T[Task]
T -->|Fail + Retry| H
H -->|Promote| H
T -->|Success| CB[Callback]
T -->|Panic| R[Recovery → Error]
```

## File Structure

```
go-queue/
├── new.go # Queue construction, worker startup and shutdown
├── task.go # Task struct and min-heap implementation
├── pending.go # Pending queue, push/pop and auto-promotion
├── priority.go # Priority level definitions and timeout calculation
├── option.go # Enqueue options (TaskID, Timeout, Callback, Retry)
├── main_test.go # Tests
├── go.mod
└── LICENSE
App[Application] --> Enqueue[Enqueue]
Enqueue --> Pending[Pending Heap]
Pending --> Workers[Worker Pool]
Workers --> Execute[Execute / Timeout / Retry]
App --> Start[Start]
App --> Shutdown[Shutdown]
Start --> Workers
Shutdown --> Pending
```

## License
Expand All @@ -69,20 +57,13 @@ This project is licensed under the [MIT LICENSE](LICENSE).

## Author

<img src="https://avatars.githubusercontent.com/u/25631760" align="left" width="96" height="96" style="margin-right: 0.5rem;">
<img src="https://github.com/pardnchiu.png" align="left" width="96" height="96" style="margin-right: 0.5rem;">

<h4 style="padding-top: 0">邱敬幃 Pardn Chiu</h4>

<a href="mailto:dev@pardn.io" target="_blank">
<img src="https://pardn.io/image/email.svg" width="48" height="48">
</a> <a href="https://linkedin.com/in/pardnchiu" target="_blank">
<img src="https://pardn.io/image/linkedin.svg" width="48" height="48">
</a>

## Stars

[![Star](https://api.star-history.com/svg?repos=pardnchiu/go-queue&type=Date)](https://www.star-history.com/#pardnchiu/go-queue&Date)
<a href="mailto:hi@pardn.io">hi@pardn.io</a><br>
<a href="https://www.linkedin.com/in/pardnchiu">https://www.linkedin.com/in/pardnchiu</a>

***

©️ 2025 [邱敬幃 Pardn Chiu](https://linkedin.com/in/pardnchiu)
©️ 2025 [邱敬幃 Pardn Chiu](https://www.linkedin.com/in/pardnchiu)
88 changes: 0 additions & 88 deletions README.zh.md

This file was deleted.

2 changes: 1 addition & 1 deletion main_test.go → core/main_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// * generated by claude sonnet 4.5
package goQueue
package core

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion new.go → core/new.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goQueue
package core

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion option.go → core/option.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goQueue
package core

import "time"

Expand Down
2 changes: 1 addition & 1 deletion pending.go → core/pending.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goQueue
package core

import (
"container/heap"
Expand Down
2 changes: 1 addition & 1 deletion priority.go → core/priority.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goQueue
package core

import (
"time"
Expand Down
2 changes: 1 addition & 1 deletion task.go → core/task.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package goQueue
package core

import (
"context"
Expand Down
Binary file removed cover.png
Binary file not shown.
Loading
Loading