gpool

package
v0.0.0-...-e89c278 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MPL-2.0 Imports: 9 Imported by: 5

README

gpool

一个管理协程池和对协程池进行资源分配的工具包

推荐在需要进行全局资源分配的需求中使用,会做协程资源的释放和重分配,所以同时不推荐在固定协程资源的需求中使用

使用方式
后台持续运行的协程池
package main

import (
	"github.com/darabuchi/utils/gpool"
)

func main()  {
	pool := gpool.NewPoolGlobalWithFunc("test_pool", 5, func(i interface{}) {
		log.Info(i)
	})
	pool.SetAlways()

	pool.Submit(1)
}

关心执行结果
package main

import (
	"github.com/darabuchi/utils/gpool"
)

func main()  {
	pool := gpool.NewPoolGlobalWithFunc("test_pool", 5, func(i interface{}) {
		log.Info(i)
	})
	defer pool.Close()

	pool.SetAlways()

	pool.Submit(1)

    pool.Wait()
}
修改系统内的协程限制
package main

import (
	"github.com/darabuchi/utils/gpool"
)

func main()  {
	gpool.SetPoolGlobalMaxWorker(3)
}
同一个协程池下的子任务
package main

import (
	"github.com/darabuchi/utils/gpool"
)

func main()  {
	pool := gpool.NewPoolGlobalWithFunc("test_pool", 5, func(i interface{}) {
		log.Info(i)
	})
	subPool := pool.NewSubPool("test_sub_pool")
	subPool.Submit(1)
    subPool.SubmitWithFunc(2, func(i interface{}) {
		log.Infof("sub_pool %d", i)
	})
	subPool.Wait()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DisablePoolGlobal

func DisablePoolGlobal()

func PoolGlobalLoadTotal

func PoolGlobalLoadTotal() uint64

func SetPoolGlobalMaxWorker

func SetPoolGlobalMaxWorker(worker int)

Types

type Logic

type Logic func(i interface{})

type Pool

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

func NewPoolGlobal

func NewPoolGlobal(name string, work int) *Pool

func NewPoolGlobalWithFunc

func NewPoolGlobalWithFunc(name string, work int, logic Logic) *Pool

func (*Pool) Close

func (p *Pool) Close()

func (*Pool) NewSubPool

func (p *Pool) NewSubPool(name string) *SubPool

func (*Pool) NewSubPoolWithFunc

func (p *Pool) NewSubPoolWithFunc(name string, logic Logic) *SubPool

func (*Pool) SetAlways

func (p *Pool) SetAlways() *Pool

func (*Pool) SetDefLogic

func (p *Pool) SetDefLogic(logic Logic) *Pool

func (*Pool) SetTimeout

func (p *Pool) SetTimeout(timeout time.Duration) *Pool

func (*Pool) SetWorker

func (p *Pool) SetWorker(worker int)

func (*Pool) Submit

func (p *Pool) Submit(i interface{})

func (*Pool) SubmitWait

func (p *Pool) SubmitWait(i interface{})

func (*Pool) SubmitWithFunc

func (p *Pool) SubmitWithFunc(i interface{}, logic Logic)

func (*Pool) SubmitWithFuncWait

func (p *Pool) SubmitWithFuncWait(i interface{}, logic Logic)

func (*Pool) SubmitWithTimeout

func (p *Pool) SubmitWithTimeout(i interface{}, timeout time.Duration)

func (*Pool) Wait

func (p *Pool) Wait()

func (*Pool) WaitChan

func (p *Pool) WaitChan() chan bool

type Statistics

type Statistics struct {
	TotalTask uint64
	TotalWork uint32
	TotalWait uint64

	WorkStatisticsMap map[string]*WorkStatistics
}

func PoolGlobalStatistics

func PoolGlobalStatistics() Statistics

type SubPool

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

func (*SubPool) Close

func (p *SubPool) Close()

func (*SubPool) LoadWaitCount

func (p *SubPool) LoadWaitCount() int64

func (*SubPool) SetLogic

func (p *SubPool) SetLogic(logic Logic)

func (*SubPool) Submit

func (p *SubPool) Submit(i interface{})

func (*SubPool) SubmitWait

func (p *SubPool) SubmitWait(i interface{})

func (*SubPool) SubmitWithFunc

func (p *SubPool) SubmitWithFunc(i interface{}, logic Logic)

func (*SubPool) SubmitWithFuncWait

func (p *SubPool) SubmitWithFuncWait(i interface{}, logic Logic)

func (*SubPool) Wait

func (p *SubPool) Wait()

type WorkPool

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

func NewPool

func NewPool(maxWorker int) *WorkPool

func (*WorkPool) Close

func (p *WorkPool) Close()

func (*WorkPool) LoadTotal

func (p *WorkPool) LoadTotal() uint64

func (*WorkPool) NewPool

func (p *WorkPool) NewPool(name string, work int) *Pool

func (*WorkPool) NewPoolWithFunc

func (p *WorkPool) NewPoolWithFunc(name string, work int, logic Logic) *Pool

func (*WorkPool) SetLogger

func (p *WorkPool) SetLogger(l *log.Logger) *WorkPool

func (*WorkPool) SetMaxWorker

func (p *WorkPool) SetMaxWorker(worker int) *WorkPool

func (*WorkPool) Statistics

func (p *WorkPool) Statistics() Statistics

type WorkStatistics

type WorkStatistics struct {
	Id   string
	Name string

	TotalTask uint64
	TotalWork uint32
	TotalWait uint64

	AvgProcessingTime time.Duration
}

Jump to

Keyboard shortcuts

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