ling-baseling-base

对象池

ling-base common/pool 模块文档

在线 Playground

在浏览器中直接体验本页相关 API,无需本地安装 Go 环境。

pool

Production-grade object pools, connection pools, and goroutine worker pools.

Components

ObjectPool — Generic Object Pool

A generic, thread-safe object pool with configurable max open objects.

pool := pool.NewObjectPool(func() (*sql.Conn, error) {
    return db.Conn(ctx)
}, 10, func(c *sql.Conn) { c.Close() })

conn, err := pool.Get()
defer pool.Put(conn)

stats := pool.Stats() // OpenCount, IdleCount, InUse

ConnPool — Connection Pool with Health Check + Idle Eviction

Extends the object pool with:

  • Health check on Get — unhealthy connections are destroyed and replaced
  • Max lifetime — connections older than MaxLifetime are recycled
  • Idle eviction — a background janitor evicts connections idle longer than MaxIdleTime
pool := pool.NewConnPool(factory, destroyer, pool.ConnConfig{
    MaxOpen:       20,
    MaxIdle:       10,
    MaxIdleTime:   5 * time.Minute,
    MaxLifetime:   30 * time.Minute,
    HealthCheck:   func(c *sql.Conn) error { return c.PingContext(ctx) },
    HealthPeriod:  1 * time.Minute,
})

WorkerPool — Goroutine Worker Pool

A fixed-size goroutine pool with a buffered task queue and graceful shutdown.

wp := pool.NewWorkerPool(8, 1000) // 8 workers, 1000 task buffer
wp.Start()

err := wp.Submit(func() {
    // do work
})

wp.Stop() // graceful — waits for all tasks to complete

License

MIT

On this page