Documentation
¶
Overview ¶
Package gopool provides a strand-based goroutine pool.
The core idea is simple:
- A Pool owns a fixed number of strands.
- Each strand executes tasks sequentially.
- A Handle is a stable reference to a single strand.
- Tasks scheduled through the same Handle never overlap and preserve submission order.
- Tasks scheduled through different Handles may run concurrently.
This model is useful for stateful, order-sensitive workloads where per-actor or per-key serialization is required without global locking.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrPoolClosed = errors.New("pool is shut down")
ErrPoolClosed is returned when scheduling or shutdown is attempted after the pool has entered draining mode.
Functions ¶
This section is empty.
Types ¶
type Handle ¶
type Handle struct {
// contains filtered or unexported fields
}
Handle represents a stable execution context.
Tasks scheduled through the same Handle are guaranteed to execute sequentially and in submission order.
type Option ¶
type Option func(*Pool)
Option configures a Pool at construction time.
func WithPanicHandler ¶
func WithPanicHandler(h PanicHandler) Option
WithPanicHandler installs a handler invoked when a task panics.
type PanicHandler ¶
PanicHandler is invoked when a task panics.
The pool itself does not log, crash, or restart. If no PanicHandler is provided, panics are silently recovered.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is a fixed-size collection of execution strands.
A Pool creates all goroutines eagerly at construction time. It does not spawn goroutines per task.
Example (Basic) ¶
package main
import (
"context"
"fmt"
"github.com/codegrapple/gopool"
)
func main() {
ctx := context.Background()
// Pool with bounded parallelism.
pool := gopool.New(2, 8)
defer pool.Shutdown(ctx)
// Two independent handles, each bound to a single goroutine.
h1 := pool.NewHandle()
h2 := pool.NewHandle()
var (
seq1, seq2 int
out1, out2 []int
)
// Schedule work concurrently, but observe sequential execution per handle.
for i := 0; i < 3; i++ {
h1.Schedule(ctx, func() {
out1 = append(out1, seq1)
seq1++
})
h2.Schedule(ctx, func() {
out2 = append(out2, seq2)
seq2++
})
}
// Ensure all scheduled work has completed.
pool.Shutdown(ctx)
fmt.Println(out1)
fmt.Println(out2)
}
Output: [0 1 2] [0 1 2]
func New ¶
New constructs a new Pool.
size controls the number of independent strands. queue controls the per-strand task queue capacity.
Panics if size <= 0 or queue < 0.