Help us shape the future of Go! Take the 2026 Go Developer Survey

triggered

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 4, 2023 License: MIT Imports: 3 Imported by: 0

README

Event Executor

Event Executor是一个基于事件触发的执行器。它可以注册执行函数和错误恢复函数,通过外部事件通知触发执行。

特性

  • 支持事件通知触发执行
  • 执行前检查通知计数阈值
  • 可控制最大并发执行数
  • 执行错误恢复保护
  • 共享参数传递

用法

1. 定义执行函数

执行函数签名为:

func(params *Params[T])

T是参数类型。

2. 注册执行函数

使用RegisterExecute注册:

exec := RegisterExecute[T](execFunc)
3. 配置(可选)

可以链式配置执行单元:

exec.WithRecover(recoverFunc).WithShared(sharedData).WithConcurrentNum(10) 
  • WithRecover 指定错误恢复函数
  • WithShared 提供共享参数
  • WithConcurrentNum 最大并发数
4. 触发执行

调用Notify,传递参数:

exec.Notify(params)

达到阈值时会异步执行execFunc。

执行前会检查并发数是否超限。

TODO

  • 提供启动/停止方法
  • 增加测试用例

欢迎提出改进意见!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EventExecute added in v0.0.3

type EventExecute[PARAMS any] struct {
	NULL PARAMS
	// contains filtered or unexported fields
}

EventExecute封装了一个执行单元, 默认是不启动,需要Notify通知触发. 属于被动触发式 包含执行函数,恢复函数和相关控制参数

func RegisterExecute added in v0.0.3

func RegisterExecute[PARAMS any](execDo func(params *Params[PARAMS])) *EventExecute[PARAMS]

RegisterExecute注册一个执行单元 返回分配的事件号

func (*EventExecute[PARAMS]) Notify added in v0.0.3

func (exec *EventExecute[PARAMS]) Notify(params PARAMS)

Notify用于通知触发执行 根据事件号查找执行单元并检查通知次数 达到指定次数则触发goroutine异步执行

func (*EventExecute[PARAMS]) RefCountAdd added in v0.2.2

func (exec *EventExecute[PARAMS]) RefCountAdd(count int64) int64

RefCountAdd 引用计数, 如果引用为0, 则不触发. 返回当前的引用计数

func (*EventExecute[PARAMS]) WithConcurrentNum added in v0.1.0

func (e *EventExecute[PARAMS]) WithConcurrentNum(v int64) *EventExecute[PARAMS]

func (*EventExecute[PARAMS]) WithRecover added in v0.1.0

func (e *EventExecute[PARAMS]) WithRecover(recoverDo func(ierr any)) *EventExecute[PARAMS]

func (*EventExecute[PARAMS]) WithShared added in v0.1.0

func (e *EventExecute[PARAMS]) WithShared(v any) *EventExecute[PARAMS]

type Params

type Params[T any] struct {
	Value  T
	Shared *Shared
}

type Shared added in v0.0.3

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

func (*Shared) SetValue added in v0.0.3

func (s *Shared) SetValue(v any)

func (*Shared) Value added in v0.0.3

func (s *Shared) Value(do func(v any))

Jump to

Keyboard shortcuts

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