Skip to content

morsuning/lifopool

Repository files navigation

LifoPool

go-pool

GoDoc Go Report Card License

High-Performance, Feature-Rich, and Production-Ready Goroutine Pool for Go.

English | 中文文档


🚀 Introduction

lifopool is a library designed to manage specific goroutine workers to handle tasks concurrently. It reuses goroutines to limit concurrency, reduce resource consumption, and improve the stability and performance of your applications.

Key differentiators:

  • LIFO Scheduling: Improves CPU cache locality by prioritizing recently used workers.
  • Dynamic Resizing: Automatically scales workers up or down based on traffic load.
  • Robustness: Built-in panic recovery, timeout handling, and retry mechanisms.

📊 Benchmarks

lifopool is optimized for speed and efficiency. Below are benchmark results comparing it with other popular libraries (ants, pond) and raw goroutines.

Library Optimization Time (ns/op) Memory (B/op) Allocations (allocs/op)
lifopool Default 1,114,703,667 2,757,944 14,906
ants - 1,141,786,333 4,533,200 59,463
pond - 1,479,714,792 1,035,432 10,788
Raw Goroutines None 336,763,680 128,759,930 3,007,120

Highlights:

  • Faster than ants and pond in handling 1 million tasks.
  • 50% Less Memory usage compared to ants.
  • Significant reduction in allocations compared to raw goroutines and ants.

Benchmarks run on Apple M3, processing 1M tasks.

✨ Features

  • 🚀 High Performance: Low overhead, LIFO stack-based worker management.
  • ⚖️ Dynamic Scaling: Auto-scale worker count based on queue monitoring.
  • 🛡️ Panic Recovery: Automatically recovers from worker panics to prevent leaks.
  • ⏱️ Timeout Support: Task execution timeouts with context integration.
  • 🔄 Retry Mechanism: Configurable retries for failed tasks.
  • 🔒 Custom Lock: Standard sync.Mutex or high-performance SpinLock support.

📦 Installation

go get -u github.com/morsuning/lifopool

⚡ Quick Start

Simple Usage

package main

import (
    "fmt"
    "time"
    "github.com/morsuning/lifopool"
)

func main() {
    pool := lifopool.New(100)
    defer pool.Release()

    pool.AddTask(func() (any, error) {
        time.Sleep(10 * time.Millisecond)
        fmt.Println("Hello, lifopool!")
        return nil, nil
    })
    
    pool.Wait()
}

See examples/ for more usage patterns, including Custom Locks and Timeout handling.

⚙️ Configuration

lifopool.New accepts functional options to tailor behavior:

Option Description Default
WithMinWorkers(n) Minimum idle workers to keep. maxWorkers
WithTaskQueueSize(n) Size of the buffered task channel. 1e6
WithTimeout(d) Execution timeout for tasks. 0 (None)
WithRetryCount(n) Retries upon failure. 0
WithErrorCallback(fn) Callback for task errors/panics. nil
WithLock(l) Custom lock implementation. sync.Mutex

🤝 Contributing

Contributions are welcome! Please check out the issues or submit a PR.

📄 License

MIT © morsuning

About

A high-performance, LIFO-scheduled goroutine pool for Go with dynamic auto-scaling and superior CPU cache affinity.

Resources

License

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages