exegroup

package module
v0.0.0-...-ea49ff6 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2023 License: MIT Imports: 10 Imported by: 0

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

func HTTPListenAndServe

func HTTPListenAndServe(port int, handler http.Handler) (func(ctx context.Context) error, func(ctx context.Context))

HTTPListenAndServe 提供 http.Server 的启动和停止函数;

func HandleSignal

func HandleSignal(signals ...os.Signal) func(ctx context.Context) error

HandleSignal 提供信号处理函数;

Types

type Actor

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

Actor 是 Group 调度的执行单元;

Actor.stopFunc 可以是 nil, 这时 Actor 需要受 goFunc 的 ctx 控制退出;

func (*Actor) WithGo

func (actor *Actor) WithGo(goFunc func(ctx context.Context) error) *Actor

WithGo 指定 Actor 的 goFunc; 通过 WithGo 注册的 goFunc 函数应该受 ctx 控制退出;

func (*Actor) WithGoStop

func (actor *Actor) WithGoStop(goFunc func(ctx context.Context) error, stopFunc func(ctx context.Context)) *Actor

WithGoStop 指定 Actor 的 goFunc 和 stopFunc; goFunc 不受 ctx 控制退出, 而是在 stopFunc 调用后退出;

  1. goFunc 被 Group 在 goroutine 中启动;
  2. stopFunc 在被 Group 启动的任意 goFunc 返回后被调用;

使用 stopFunc 可以在 stopFunc 的 ctx 中指定等待期限, 让 goFunc 延迟到等待期限强制退出;

func (*Actor) WithName

func (actor *Actor) WithName(name string) *Actor

WithName 指定 Actor 的 name;

type Group

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

Group 管理1组 Actor, 每个 Actor 在1个goroutine 中运行; Group 的执行过程参考 Group.Run;

Group 的配置包含:

func Default

func Default(opts ...Option) *Group

Default 创建包含信号处理 ActorGroup;

func New

func New(opts ...Option) *Group

New 创建 Group;

func (*Group) New

func (g *Group) New() *Actor

New 创建并添加 Actor, 对 Actor 的配置通过链式调用完成;

func (*Group) Run

func (g *Group) Run(ctx context.Context) error

Run 启动所有 [Actor]s 并等待 [Actor]s 执行完成; 当没有 Actor 时执行 Run 会 panic;

Run 包含两个阶段:

  1. 启动 Actor 并等待, Actor 的运行函数返回错误时进入下1个阶段;
  2. 执行 Actor 的终止函数并返回;

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option 修改 Group 的配置;

func WithConcurrentStop

func WithConcurrentStop() Option

WithConcurrentStop 并发执行 Actor 的终止函数;

func WithStopTimeout

func WithStopTimeout(d time.Duration) Option

WithStopTimeout 指定 Group.Run 从进入终止过程到返回的最长时间;

Jump to

Keyboard shortcuts

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