gopool

package module
v0.0.0-...-c21759e Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 4 Imported by: 1

README

gopool

A simple goroutine pool.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Go

func Go(fn func())

Run fn in another goroutine.

func GoCreated

func GoCreated() uint64

func GoReused

func GoReused() uint64

func Size

func Size() int

Types

type Job

type Job[T any] struct {
	Fn   func(T)
	Args T
}

type Pool

type Pool[T any] struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool[T any]() *Pool[T]

NewPool creates a new goroutine pool. It is recommended to use the global funcs Go() in most cases. Creating your own pool for a specific func signature can eliminate the allocation of args. Idled goroutines will be removed automatically. The pace is depending on the runtime GC. After the GC finishing, all workers will be marked as idle. After the GC finishing again, workers that are still marked as idle will be removed. This logic is kind same as sync.Pool.

func (*Pool[T]) Close

func (p *Pool[T]) Close()

Close the pool gc goroutine and all idled workers. It won't stop any busy worker. After the pool being closed, GoArgs() can still be called but it will fallback to "go".

func (*Pool[T]) GoCreated

func (p *Pool[T]) GoCreated() uint64

The number of started workers/goroutines.

func (*Pool[T]) GoJob

func (p *Pool[T]) GoJob(j Job[T])

func (*Pool[T]) GoReused

func (p *Pool[T]) GoReused() uint64

The number of reused goroutines.

func (*Pool[T]) Size

func (p *Pool[T]) Size() (n int)

Current pool size (aka. the number of current idled workers).

Jump to

Keyboard shortcuts

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