pool

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2024 License: MIT Imports: 4 Imported by: 0

README

goroutine-pool

Go Coverage

A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.

install

go get -u -v github.com/sysulq/goroutine-pool

example

package main

import (
	"fmt"
	pool "github.com/sysulq/goroutine-pool"
	"sync"
)

func main() {
	pool.Start()
	defer pool.Stop()

	wg := sync.WaitGroup{}
	for i := 0; i < 5; i++ {
		wg.Add(1)
		pool.Go(func() {
			fmt.Println(i)
			wg.Done()
		})
	}
	wg.Wait()
	fmt.Println()

	for i := 0; i < 5; i++ {
		wg.Add(1)
		n := i
		pool.Go(func() {
			fmt.Println(n)
			wg.Done()
		})
	}
	wg.Wait()
}
$ go run main.go
5
5
5
5
5

4
1
0
2
3

benchmarks

With Wait

Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutine$

goos: windows
goarch: amd64
pkg: workerpool
BenchmarkGoroutine-4   	    1000	   1634621 ns/op	      65 B/op	       1 allocs/op
PASS
ok  	workerpool	2.047s
Success: Benchmarks passed.
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPool$

goos: windows
goarch: amd64
pkg: workerpool
BenchmarkPool-4   	    2000	   1146818 ns/op	      17 B/op	       1 allocs/op
PASS
ok  	workerpool	2.702s
Success: Benchmarks passed.

Without Wait

cpu run at 100%
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutineWithoutWait$

goos: windows
goarch: amd64
pkg: workerpool
BenchmarkGoroutineWithoutWait-4   	 1000000	      4556 ns/op	     517 B/op	       1 allocs/op
PASS
ok  	workerpool	5.649s
Success: Benchmarks passed.
cpu relatively low
Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPoolWithoutWait$

goos: windows
goarch: amd64
pkg: workerpool
BenchmarkPoolWithoutWait-4   	10000000	       144 ns/op	       3 B/op	       0 allocs/op
PASS
ok  	workerpool	4.812s
Success: Benchmarks passed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CoarseTimeNow

func CoarseTimeNow() time.Time

CoarseTimeNow returns the current time truncated to the nearest second.

This is a faster alternative to time.Now().

func Go

func Go(c func())

Types

type WorkerPool

type WorkerPool struct {
	MaxWorkersCount int

	MaxIdleWorkerDuration time.Duration
	// contains filtered or unexported fields
}

WorkerPool serves outgoing connections via a pool of workers in FILO order, i.e. the most recently stopped worker will serve the next incoming connection.

Such a scheme keeps CPU caches hot (in theory).

func (*WorkerPool) Go

func (wp *WorkerPool) Go(c func()) bool

func (*WorkerPool) Start

func (wp *WorkerPool) Start()

func (*WorkerPool) Stop

func (wp *WorkerPool) Stop()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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