gothrottler

package
v0.0.0-...-5c98b36 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2022 License: MulanPSL-2.0 Imports: 2 Imported by: 0

README

gothrottler Go Routine 限流器

用于控制 Goroutine 并发数

Example:

func main() {
	th := gothrottler.New(3) // 最大并发数为3

	for i := 0; i < 20; i++ {
		fn := func(args ...interface{}) {
			time.Sleep(time.Duration(randx.Integer(500, 2000)) * time.Millisecond)

			idx := args[0].(int)
			fmt.Printf("GoRoutine#%d: Now = %s\n", idx, time.Now().Format("15:04:05.9999"))
		}

		th.AddJob(fn, i)
	}

	th.Wait() // 等待所有任务结束
}

Documentation

Overview

Package gothrottler 用于控制 Goroutine 并发数

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Throttler

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

Throttler goroutine 限流器

func New

func New(maxWorks int) *Throttler

New 创建一个 goroutine 限流器实例

func (*Throttler) AddJob

func (t *Throttler) AddJob(fn func(args ...interface{}), fnArgs ...interface{})

AddJob 添加一个任务

func (*Throttler) InterruptAndWait

func (t *Throttler) InterruptAndWait()

InterruptAndWait 取消还没有执行的任务,清空任务列队,并等待正在执行的任务完成

func (*Throttler) Wait

func (t *Throttler) Wait()

Wait 等待全部任务完成

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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