Documentation
¶
Index ¶
- Variables
- func DefaultConfig() *config
- func Go(ctx context.Context, fn func(context.Context), handler ErrorHandler)
- func Join[T any](ctx context.Context, futures ...*Future[T]) ([]T, error)
- func Safely(ctx context.Context, fn func(context.Context), handler ErrorHandler) (panicked bool)
- type ErrorHandler
- type Future
- type Option
- type Runner
- type Stats
- type TaskFunc
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrQueueFull 表示任务队列已满,无法接受新任务。 // 调用者应该处理此错误(例如:返回 503,或者降级)。 ErrQueueFull = errors.New("task: queue is full") // ErrRunnerClosed 表示 Runner 已经停止或正在停止,不再接受新任务。 ErrRunnerClosed = errors.New("task: runner is closed") )
Functions ¶
func Go ¶
func Go(ctx context.Context, fn func(context.Context), handler ErrorHandler)
Go 是 go func() 的安全替代品。 它启动一个新的协程并执行 fn,自带 Panic 保护。
Types ¶
type ErrorHandler ¶
ErrorHandler 用于处理任务执行过程中的 Panic 或错误。 ctx: 发生错误时的上下文 p: recover() 捕获的对象 (可能为 nil)
type Future ¶ added in v1.4.1
type Future[T any] struct { // contains filtered or unexported fields }
Future 是一个轻量级的句柄,只持有结果和信号通道 相比 Promise,它没有锁,没有回调链,内存占用极小
type Option ¶
type Option func(*config)
Option 定义配置修改函数
func WithErrorHandler ¶
func WithErrorHandler(h ErrorHandler) Option
WithErrorHandler 设置 Panic 处理回调。 建议在此处集成 zap/zerolog 等日志库。
type Runner ¶
type Runner struct {
// contains filtered or unexported fields
}
Runner 是一个并发限制的任务执行器。
type Stats ¶
type Stats struct {
// 配置参数
MaxWorkers int `json:"max_workers"`
QueueSize int `json:"queue_size"`
// 实时状态
ActiveWorkers int64 `json:"active_workers"` // 当前正在执行任务的 Worker 数
QueuedTasks int `json:"queued_tasks"` // 当前排队等待的任务数
// 累积计数 (自启动以来)
TotalProcessed uint64 `json:"total_processed"` // 成功处理的任务数
TotalPanics uint64 `json:"total_panics"` // 发生的 Panic 总数
TotalRefused uint64 `json:"total_refused"` // 因队列满被拒绝的任务数
}
Stats 包含 Runner 的运行时快照
Click to show internal directories.
Click to hide internal directories.