rpool

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: Apache-2.0 Imports: 2 Imported by: 2

README

RPOOL - routines pool for Golang

Build Status Go Report Card GoDoc Coverage Status

License Apache 2.0

Extension of execution pools over native goroutines on the pure GO.

import "github.com/demdxx/rpool"

Random task pool example

pool := NewPool()
defer pool.Close()

pool.Go(func(){
  atomic.AddInt64(&iterations, 1)
})

Function task pool example

To process some predefined executor over the concurrent queue.

pool := NewPoolFunc(func(arg interface{}) {
  atomic.AddInt64(arg.(*int64), 1)
})
defer pool.Close()

pool.Call(&iterations)

Execute task once at a time

In case of data refreshing we need to execute only one task and no any other until the provious will be completed. All new tasks will be skipet.

datastore := userStore.New()
dataStoreUpdate := NewSinglePoolFunc(func(arg interface{}) {
  datastore.Refresh()
})
defer pool.Close()

...

if !dataStoreUpdate.Call() {
  // Call is ignored because one update task in process
}

Benchmarks

Running tool: go test -benchmem -run=^$ github.com/demdxx/go-rpool -bench . -v -race

goos: darwin
goarch: amd64
pkg: github.com/demdxx/rpool
Benchmark_PoolFunc
Benchmark_PoolFunc-8     	  340465	      3028 ns/op	       0 B/op	       0 allocs/op
Benchmark_NoPoolFunc
Benchmark_NoPoolFunc-8   	   22674	     66280 ns/op	       2 B/op	       0 allocs/op
Benchmark_Pool
Benchmark_Pool-8         	   58316	     21381 ns/op	      32 B/op	       1 allocs/op
Benchmark_NoPool
Benchmark_NoPool-8       	   20587	     59611 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/demdxx/rpool	7.948s

On MacOS M1 ARM

goos: darwin
goarch: arm64
pkg: github.com/demdxx/rpool
Benchmark_PoolFunc
Benchmark_PoolFunc-8              237615              4707 ns/op               0 B/op          0 allocs/op
Benchmark_NoPoolFunc
Benchmark_NoPoolFunc-8             45610             24663 ns/op               6 B/op          0 allocs/op
Benchmark_Pool
Benchmark_Pool-8                   24295             47394 ns/op              24 B/op          1 allocs/op
Benchmark_NoPool
Benchmark_NoPool-8                 33097             33844 ns/op               2 B/op          0 allocs/op
PASS
ok      github.com/demdxx/rpool 6.066s

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(opt *PoolOption)

Option func type which adjust option values

func WithMaxTasksCount

func WithMaxTasksCount(cnt int) Option

WithMaxTasksCount defines maximum amount of tasks prepared for execution

func WithRecoverHandler

func WithRecoverHandler(f func(interface{})) Option

WithRecoverHandler defined error handler

func WithWorkerCount

func WithWorkerCount(count int) Option

WithWorkerCount change count of workers

func WithWorkerPoolSize

func WithWorkerPoolSize(size int) Option

WithWorkerPoolSize setup maximal size of worker pool

type Pool

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

Pool provides common queue of tasks execution

func NewPool

func NewPool(options ...Option) *Pool

NewPool of task processing

func NewSinglePool

func NewSinglePool(options ...Option) *Pool

NewSinglePool for one task simultaneusly processing

func (*Pool) Close

func (pool *Pool) Close() error

Close of the pool and all workers. Tasks can be finished later

func (*Pool) Go

func (pool *Pool) Go(f func()) bool

Go sends function task into the queue

func (*Pool) InProcess

func (pool *Pool) InProcess() int64

InProcess returns count of tasks in process“

type PoolFunc

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

PoolFunc concurrent async processor with single handler

func NewPoolFunc

func NewPoolFunc(fnk func(interface{}), options ...Option) *PoolFunc

NewPoolFunc returns function pool

func NewSinglePoolFunc

func NewSinglePoolFunc(fnk func(interface{}), options ...Option) *PoolFunc

NewSinglePoolFunc returns function pool one task simultaneusly processing

func (*PoolFunc) Call

func (pool *PoolFunc) Call(arg interface{}) bool

Call new task with the arg

func (*PoolFunc) Close

func (pool *PoolFunc) Close() error

Close of the pool and all workers. Tasks can be finished later

func (*PoolFunc) InProcess

func (pool *PoolFunc) InProcess() int64

InProcess returns count of tasks in process

type PoolOption

type PoolOption struct {
	WorkerCount    int
	WorkerPoolSize int
	MaxTasksCount  int
	RecoverHandler func(interface{})
}

PoolOption contains options of the pool

func (*PoolOption) PreparedWorkerCount

func (opt *PoolOption) PreparedWorkerCount() int

PreparedWorkerCount returns maximum count ow workers or Num of CPU

func (*PoolOption) TaskQueueSize

func (opt *PoolOption) TaskQueueSize() int

TaskQueueSize returns the common pool size for all workers

Jump to

Keyboard shortcuts

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