pool

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: MIT Imports: 2 Imported by: 0

README

eeianux/pool

LICENSE Language Go Report Card Coverage

a simple go routine pool

Installation

go get github.com/eeianux/pool
go mod tidy

Quick start

Make a test, for example:

package pool

import (
	. "github.com/smartystreets/goconvey/convey"
	"sync/atomic"
	"testing"
	"time"
)

func TestPool(t *testing.T) {
	Convey("pool", t, func() {
		const cnt = 1000
		pool := NewPool(cnt / 2)
		start := time.Now()
		for i := 0; i < cnt; i++ {
			pool.Go(func() {
				time.Sleep(time.Second)
			})
		}
		pool.Close()
		So(time.Since(start)/time.Millisecond-time.Second*2/time.Millisecond, ShouldBeLessThan, 10)
	})
	Convey("panic", t, func() {
		const cnt = 10
		pool := NewPool(cnt / 2)
		start := time.Now()
		for i := 0; i < cnt; i++ {
			pool.Go(func() {
				time.Sleep(time.Second)
				panic("test")
			})
		}
		pool.Close()
		So(time.Since(start)/time.Millisecond-time.Second*2/time.Millisecond, ShouldBeLessThan, 10)
	})
	Convey("wait", t, func() {
		const cnt = 10
		tt := atomic.Int64{}
		tt.Store(0)
		pool := NewPool(cnt)
		for i := 0; i < cnt; i++ {
			pool.Go(func() {
				tt.Add(1)
			})
		}
		pool.Close()
		So(tt.Load(), ShouldEqual, cnt)
	})
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pool

type Pool interface {
	Go(func())
	Close()
}

func NewPool

func NewPool(cnt int) Pool

Jump to

Keyboard shortcuts

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