gp

package module
v0.0.0-...-4f9e4f1 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2023 License: Apache-2.0 Imports: 1 Imported by: 3

README

A simple goroutine pool

Rational

Goroutine is cheap, but not free. Especially when the goroutine trigger runtime.morestack, the cost become high.

This package is mainly aimed to handle that. Put back the stack-growed goroutine to a pool, and reuse that goroutine can eliminate the runtime.morestack cost.

See a blog post (Chinese) https://www.zenlife.tk/goroutine-pool.md

Usage

Just replace your go f() call with gp.Go(f):

import "github.com/tiancaiamao/gp"

var gP = gp.New(N, time.Duration)

gp.Go(func() {
	// ...
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

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

func New

func New(n int, dur time.Duration) *Pool

New create a new goroutine pool. The pool size is n, means that it will keep at most n goroutines in the pool. The dur parameter controls the idle recycle behaviour. If the goroutine in the pool is idle for a while, it will be recycled.

func (*Pool) Close

func (p *Pool) Close()

Close releases the goroutines in the Pool. After this operation, inflight tasks may still execute until finish. But all the new coming tasks will be simply ignored.

func (*Pool) Go

func (p *Pool) Go(f func())

Run execute the function in a seperate goroutine,

Jump to

Keyboard shortcuts

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