Documentation
¶
Overview ¶
Package dag 提供一个轻量的有向无环图(DAG)执行器: 按依赖关系将节点拓扑分层,同一层内的节点并行执行,层间串行。 不绑定任何数据库 / 调度器 / 任务体系——每个节点的工作由 Node.Run 闭包描述。
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type DAG ¶
type DAG struct {
// contains filtered or unexported fields
}
DAG 是一组按依赖关系执行的节点。零值不可用,请用 New 构造。
Example ¶
// build -> [test, lint] -> deploy
d := New().Add(
Node{Name: "build", Run: func(ctx context.Context) error { return nil }},
Node{Name: "test", DependsOn: []string{"build"}, Run: func(ctx context.Context) error { return nil }},
Node{Name: "lint", DependsOn: []string{"build"}, Run: func(ctx context.Context) error { return nil }},
Node{Name: "deploy", DependsOn: []string{"test", "lint"}, Run: func(ctx context.Context) error { return nil }},
)
if err := d.Run(context.Background()); err != nil {
panic(err)
}
type Node ¶
type Node struct {
// Name 节点唯一标识,用于被其它节点在 DependsOn 中引用。
Name string
// DependsOn 前置依赖节点名;这些节点全部成功(或在 ContinueOnError 下执行完)后本节点才会运行。
DependsOn []string
// Run 节点要执行的工作。为 nil 时视为空节点(仅占位/聚合依赖)。
Run func(ctx context.Context) error
}
Node 是 DAG 中的一个工作单元及其依赖。
type Option ¶
type Option func(*DAG)
Option 配置 DAG。
func WithMaxParallel ¶
WithMaxParallel 限制同一层内并发执行的节点数(信号量),避免大扇出层 一次性起成千上万个 goroutine。n<=0(默认)表示不限制。
Click to show internal directories.
Click to hide internal directories.