sgpool

package module
v0.0.0-...-1db6743 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 4 Imported by: 0

README

sgpool

介绍

协程池

架构

提供两种协程池,可复用/动态创建销毁,debug模式下可以对任务的超时,失败进行统计

安装
go get gitee.com/xiawucha365/sgpool
使用

具体的worker 需要实现 WorkerInterface 接口

type WorkerInterface interface {
	Task() error
	GetTaskID() interface{}
}
常驻复用协程池
package main

import (
	"fmt"
	"math/rand"
	"gitee.com/xiawucha365/sgpool"
	"time"
)


//======================woker实现===start=====================\\
type workersp struct {
	ID string
}

//要执行的任务列表

var name_slices_sp = []string{"001","002","003","004","005","006","007","008","009"}


func (m *workersp) Task() error {

	//fmt.Println("job:" + m.ID + "runing...")
	timen := rand.Intn(3)
	//fmt.Println(timen,"seconds")
	time.Sleep(time.Second * time.Duration(timen))
	fmt.Println("job:" + m.ID + "over")
	return nil
}


//获取任务id,便于
func (m *workersp) GetTaskID() interface{} {
	return m.ID
}

//======================woker实现===end=====================\\
//例子演示
func main() {

	//创建协程池
    //timeout=0 关闭超时统计 debug=true 打开模式
	spool := sgpool.NewSPool(3, cap(name_slices_sp),2,true)

	//提交任务
	for _, id := range name_slices_sp {
		np := workersp{ID: id}
		spool.Commit(&np)
	}

	spool.Release()
	time.Sleep(time.Second * 1)
}
动态创建销毁协程池
package main

import (
	"fmt"
	"math/rand"
	"gitee.com/xiawucha365/sgpool"
	"time"
)

//======================woker实现===start=====================\\
type worker struct {
	ID string
}

//要执行的任务列表

var name_slices = []string{"001", "002", "003", "004", "005", "006", "007", "008", "009", "010", "011"}

func (m *worker) Task() error {

	fmt.Println("job:" + m.ID + "runing...")
	timen := rand.Intn(3)
	//fmt.Println(timen,"seconds")
	time.Sleep(time.Second * time.Duration(timen))
	fmt.Println("job:" + m.ID + "over")
	return nil
}

//获取任务id,便于
func (m *worker) GetTaskID() interface{} {
	return m.ID
}

//======================woker实现===end=====================\\

//并发池实例
var wpool *sgpool.WPool

//例子演示
func main() {

	//创建协程池
    //timeout=0 关闭超时统计 debug=true 打开模式
	wpool = sgpool.NewWPool(100, cap(name_slices), 0, true)

	//提交任务
	for _, id := range name_slices {
		np := worker{ID: id}
		wpool.Commit(&np)
	}

	wpool.Release()

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MathDecimal

func MathDecimal(value float64) float64

浮点保留2位小数

Types

type Pool

type Pool interface {
	Commit()
	Release()
	CountFail()
	CountOk()
	CountOut()
	Runtimelog()
	// contains filtered or unexported methods
}

type SPool

type SPool struct {
	TotalNum    int
	CounterOk   int
	CounterFail int
	CounterOut  int

	TimeStart int64
	TimeOut   int
	Debug     bool
	// contains filtered or unexported fields
}

/共享协程池

func NewSPool

func NewSPool(workerNum int, totalNum int, timeout int, debug bool) *SPool

协程池 timeout=0 关闭超时统计 debug=true 打开模式

func (*SPool) Commit

func (p *SPool) Commit(w WorkerInterface)

提交任务

func (*SPool) CountFail

func (p *SPool) CountFail()

计数器-失败

func (*SPool) CountOk

func (p *SPool) CountOk()

计数器-执行成功

func (*SPool) CountOut

func (p *SPool) CountOut()

计数器-超时

func (*SPool) Release

func (p *SPool) Release()

等待组 关闭channel

func (*SPool) Runtimelog

func (p *SPool) Runtimelog()

log

type WPool

type WPool struct {
	TotalNum    int
	CounterOk   int
	CounterFail int
	CounterOut  int

	TimeStart int64
	TimeOut   int
	Debug     bool
	// contains filtered or unexported fields
}

非共享协程池

func NewWPool

func NewWPool(workerNum int, totalNum int, timeout int, debug bool) *WPool

func (*WPool) Commit

func (p *WPool) Commit(w WorkerInterface)

提交任务

func (*WPool) CountFail

func (p *WPool) CountFail()

计数器-失败

func (*WPool) CountOk

func (p *WPool) CountOk()

计数器-执行成功

func (*WPool) CountOut

func (p *WPool) CountOut()

计数器-超时

func (*WPool) Release

func (p *WPool) Release()

等待组 关闭channel

func (*WPool) Runtimelog

func (p *WPool) Runtimelog()

log

type WorkerInterface

type WorkerInterface interface {
	Task() error
	GetTaskID() interface{}
}

任务接口

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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