workers

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2021 License: Apache-2.0 Imports: 3 Imported by: 7

README

概述

一个 Golang 的 goroutine 池。

安装

go get -u github.com/aacfactory/workers

使用

type Handler struct {
}

func (h *Handler) Handle(action string, payload interface{}) {
// todo
}

workers, workErr := workers.New(&Handler{})
if workErr != nil {
// handle error
return
}

workers.Start()

if ok := workers.Execute("test", i); !ok {
// handle 'not accepted'
}

workers.Stop()
性能对比

goos: windows
goarch: amd64
pkg: github.com/aacfactory/workers
cpu: AMD Ryzen 9 3950X 16-Core Processor

workers

BenchmarkNewWorkers-32: 923985, 1173 ns/op, 11 B/op, 1 allocs/op

goroutine

BenchmarkChanWorkers-32: 64825, 16936 ns/op, 8 B/op, 0 allocs/op

参考感谢

Documentation

Index

Constants

View Source
const (
	DefaultConcurrency = 256 * 1024
	DefaultMaxIdleTime = 2 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*Options) error

func WithConcurrency added in v1.1.0

func WithConcurrency(concurrency int) Option

func WithMaxIdleTime added in v1.1.0

func WithMaxIdleTime(maxIdleTime time.Duration) Option

type Options added in v1.1.0

type Options struct {
	Concurrency int
	MaxIdleTime time.Duration
}

type UnitHandler added in v1.1.0

type UnitHandler interface {
	Handle(action string, payload interface{})
}

type Workers

type Workers interface {
	Execute(action string, payload interface{}) (ok bool)
	Start()
	Stop()
}

func New added in v1.2.0

func New(handler UnitHandler, options ...Option) (w Workers, err error)

Jump to

Keyboard shortcuts

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