gokit

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

生产消费模型的协程池

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetGoroutineID

func GetGoroutineID() uint64

获取协程ID,慎用,仅用作有时候要打印协程ID仅当调试debug看一下用。 在C++中我们通过获取线程ID开辟不通空间保证线程安全。 而golang中google官方自从1.4就取消了获取协程ID的接口,不建议照C++那样做,因为滥用协程ID会导致GC无法及时回收内存。 说透彻点就是我知道你拿协程ID想干嘛,开数组/切片/map,然后根据协程ID来索引数据,这就会导致一个协程未退出你开的这个保存了一万个协程数据的大容器及其引用数据无法回收内存。

func NewTokenBucket

func NewTokenBucket(fillInterval time.Duration, cap int) restrictor

fillInterval 每隔多久向桶中填充一个令牌,例如:time.Microsecond * 10 每 10 毫秒填充一个令牌 cap 令牌桶容量,例如: 100。两个参数合起来,每 10 毫秒填充一个令牌,1 秒刚好填满 100 个,也就是 100 的 qps

func SafeGo

func SafeGo(fn func())

封装协程异常捕获,只管一层 e.g: go SafeGo(fn) e.g: fn := func() { test("param") }; go SafeGo(fn)

Types

type Pool

type Pool struct {
	// 对外接收 task 的入口,使用管道保存 task
	EntryChannel chan *Task

	// 协程池内部的任务就绪队列
	TasksChannel chan *Task
	// contains filtered or unexported fields
}

定义池

func NewPool

func NewPool(cap int) *Pool

创建池子 cap 为池子容量,即最大开启多少个协程 worker

func (*Pool) Run

func (pool *Pool) Run()

启动协程池进行工作

type Task

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

定义 task,即要开协程去做的事情,每一个 task 都可以抽象成一个函数

func NewTask

func NewTask(fun taskFun) *Task

创建 task fun 为具体要开协程去执行的函数

Jump to

Keyboard shortcuts

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