gwp

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2025 License: MIT Imports: 3 Imported by: 5

README

Pango Worker Pool

Concurrency limiting go-routine pool. Limits the concurrency of task execution, not the number of tasks queued.

This implementation builds on ideas from the following:

Example

package main

import (
	"fmt"
	"github.com/askasoft/pango/gwp"
)

func main() {
	wp := gwp.NewWorkerPool(2, 100)
	requests := []string{"alpha", "beta", "gamma", "delta", "epsilon"}

	for _, r := range requests {
		r := r
		wp.Submit(func() {
			fmt.Println("Handling request:", r)
		})
	}

	wp.StopWait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WorkerPool

type WorkerPool struct {
	// contains filtered or unexported fields
}

WorkerPool is a collection of goroutines, where the number of concurrent goroutines processing requests does not exceed the specified maximum.

func NewWorkerPool

func NewWorkerPool(maxWorks, maxWaits int) *WorkerPool

NewWorkerPool creates and starts a pool of worker goroutines.

The maxWorks parameter specifies the maximum number of workers that can execute tasks concurrently. When there are no incoming tasks, workers are gradually stopped until there are no remaining workers.

func (WorkerPool) CurWorks

func (wp WorkerPool) CurWorks() int

CurWorks returns the current number of concurrent workers.

func (WorkerPool) MaxWorks

func (wp WorkerPool) MaxWorks() int

MaxWorks returns the maximum number of concurrent workers.

func (WorkerPool) Running

func (wp WorkerPool) Running() bool

Running returns true if this worker pool is running.

func (WorkerPool) SetIdleTimeout

func (wp WorkerPool) SetIdleTimeout(timeout time.Duration)

SetIdleTimeout set the timeout to stop a idle worker, panic if timeout < 1ms.

func (WorkerPool) SetMaxWorks

func (wp WorkerPool) SetMaxWorks(maxWorks int)

SetMaxWorks set the maximum number of concurrent workers, panic if maxWorks < 1.

func (WorkerPool) Start

func (wp WorkerPool) Start()

Start start the pool go-routine

func (WorkerPool) Stop

func (wp WorkerPool) Stop()

Stop stops the worker pool and waits for only currently running tasks to complete. Pending tasks that are not currently running are abandoned. Tasks must not be submitted to the worker pool after calling stop.

Since creating the worker pool starts at least one goroutine for the dispatcher, Stop() should be called when the worker pool is no longer needed.

func (WorkerPool) StopWait

func (wp WorkerPool) StopWait()

StopWait stops the worker pool and waits for all queued tasks tasks to complete. No additional tasks may be submitted, but all pending tasks are executed by workers before this function returns.

func (WorkerPool) Submit

func (wp WorkerPool) Submit(task func())

Submit enqueues a function for a worker to execute.

Any external values needed by the task function must be captured in a closure. Any return values should be returned over a channel that is captured in the task function closure.

Submit will block if the task wait channel is full.

As long as no new tasks arrive, one available worker is shutdown each time period until there are no more idle workers. Since the time to start new go-routines is not significant, there is no need to retain idle workers indefinitely.

func (WorkerPool) SubmitWait

func (wp WorkerPool) SubmitWait(task func())

SubmitWait enqueues the given function and waits for it to be executed.

func (WorkerPool) Working

func (wp WorkerPool) Working() bool

Working returns true if this worker pool has running task.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL