factory

package module
v0.0.0-...-a726b5f Latest Latest
Warning

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

Go to latest
Published: May 21, 2021 License: MIT Imports: 6 Imported by: 4 modules, 5 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Go语言的协程池 , 节省内存 , 减少GC压力

安装

go get github.com/letsfire/factory

用法

// 新建协程池 , 最大允许协程数20000和初始化数量8
// 协程工人繁忙不够用的情况下 , 内部实现自动扩容
// 扩容不会超过您指定的最大允许协程数量
var master = factory.NewMaster(20000, 8)

// 新建第一条工作流水线
var line1 = master.AddLine("demo.line.1", func(args interface{}) {

	// TODO 处理您的业务逻辑
	// fmt.Println(args...)

})

line1.SetPanicHandler(func(err interface) {
    // TODO 异常处理
});

// 新建第二条工作流水线
var line2 = master.AddLine("demo.line.2", func(args interface{}) {

	// TODO 处理您的业务逻辑
	// fmt.Println(args...)

})

// 根据业务场景将参数提交
for i := 0; i < 100000; i++ {
	line1.Submit(i)
}

line1.Wait() // 等待流水线全部执行完毕

for j := 0; j < 100000; j++ {
	line2.Submit(j)
}

// 协程池数量可动态调整
master.Running()            // 正在运行的协程工人数量
master.AdjustSize(100)      // 指定数量进行扩容或缩容
master.Shutdown()           // 等于 master.AdjustSize(0)

Benchmark

测试代码 , 增加 runTimes 参数原始的 Goroutine 会出现崩溃 , 使用 Factory 的表现稳定

基准测试_1

基准测试_2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Line

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

Line 工作流水线

func NewLine

func NewLine(master *Master, action func(interface{})) *Line

func (*Line) Execute

func (l *Line) Execute(args interface{})

func (*Line) SetPanicHandler

func (l *Line) SetPanicHandler(handler func(interface{}))

func (*Line) Submit

func (l *Line) Submit(args interface{})

func (*Line) Wait

func (l *Line) Wait()

type Master

type Master struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Master 管理者角色

func NewMaster

func NewMaster(maxNum, initNum int) *Master

func (*Master) AddLine

func (m *Master) AddLine(action func(interface{})) *Line

func (*Master) AdjustSize

func (m *Master) AdjustSize(newSize int)

func (*Master) Resize

func (m *Master) Resize(maxNum int)

func (*Master) Running

func (m *Master) Running() int

func (*Master) Shutdown

func (m *Master) Shutdown()

Jump to

Keyboard shortcuts

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