Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ErrQueueFull = errors.New("task queue is full")
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
MaxWorkers int // maximum number of workers
QueueSize int // task queue size
TaskTimeout time.Duration // timeout for single task
}
Config represents pool configuration
type Metrics ¶
type Metrics struct {
ActiveWorkers atomic.Int64
PendingTasks atomic.Int64
CompletedTasks atomic.Int64
FailedTasks atomic.Int64
ProcessingTime atomic.Int64 // nanoseconds
}
Metrics tracks pool's operational metrics
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool represents a worker pool
func NewPool ¶
NewPool creates a new worker pool
Usage:
// Create a task processor
processor := func(task any) error {
// Process the task here
// ...
return nil
}
// Create a worker pool configuration
cfg := &worker.Config{
MaxWorkers: 10, // Maximum number of worker goroutines
QueueSize: 100, // Maximum number of tasks that can be queued
TaskTimeout: time.Minute, // Timeout for a single task execution
}
// Create a new worker pool
pool := worker.NewPool(cfg, processor)
// Start the worker pool
pool.Start()
// Submit tasks to the pool
err := pool.Submit("task1")
if err != nil {
log.Printf("Failed to submit task: %v", err)
}
err = pool.Submit("task2")
if err != nil {
log.Printf("Failed to submit task: %v", err)
}
// Get pool metrics
metrics := pool.GetMetrics()
log.Printf("Pool metrics: %+v", metrics)
// Check if the pool is busy
if pool.IsBusy() {
log.Println("Pool is busy")
}
// Stop the worker pool
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
pool.Stop(ctx)
func (*Pool) GetMetrics ¶
GetMetrics returns the current metrics
Click to show internal directories.
Click to hide internal directories.