Documentation ¶
Overview ¶
Package exegroup 管理1组执行单元的启动和终止;
使用举例:
新建 Group:
g := New()
新建默认 Group (内置信号处理):
g := Default()
新增1个 Actor, 通过 ctx 控制 Actor 退出:
g.New().WithName("do nothing").WithGo(func(ctx context.Context) error { <-ctx.Done() return ctx.Err() })
新增1个 Actor, 通过 stopFunc 控制 Actor 退出:
server := &http.Server{Addr: ":80"} inShutdown := &atomic.Bool{} c := make(chan error, 1) goFunc := func(_ context.Context) error { err := server.ListenAndServe() if inShutdown.Load() { err = <-c } return err } stopFunc := func(ctx context.Context) { inShutdown.Store(true) c <- server.Shutdown(ctx) } g.New().WithName("server").WithGoStop(goFunc, stopFunc)
启动 Group:
g.Run()
Example (DefaultGroup) ¶
package main import ( "context" "log" "github.com/LIUKANG130/deep/exegroup" ) func main() { g := exegroup.Default() g.New().WithName("do nothing").WithGo(func(ctx context.Context) error { <-ctx.Done() return ctx.Err() }) log.Println("exit:", g.Run(context.Background())) }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Actor ¶
type Actor struct {
// contains filtered or unexported fields
}
Actor 是 Group 调度的执行单元;
Actor.stopFunc 可以是 nil, 这时 Actor 需要受 goFunc 的 ctx 控制退出;
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group 管理1组 Actor, 每个 Actor 在1个goroutine 中运行; Group 的执行过程参考 Group.Run;
Group 的配置包含:
- WithConcurrentStop 并发执行 Actor 的终止函数, 默认不并发执行;
- WithStopTimeout 指定 Group.Run 从进入终止过程到返回的最长时间, 默认不限制最长时间;
Click to show internal directories.
Click to hide internal directories.