GoWindGoWind
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
首页
框架
GoWind Toolkit
  • 开源项目

    • GoWind Admin(后台管理)
    • GoWind CMS(内容管理)
    • GoWind UBA(行为分析)
    • GoWind Shop(电商)
    • GoWind Kanban(协作看板)
    • GoWind Ledger(家庭记账)
    • GoWind ShortPlay(短剧社区)
    • GoWind Ops(运维平台)
    • RushWind(Rust 生态)
  • Open Core(社区开源 · 企业版增强)

    • GoWind OA(协同办公)
    • GoWind ERP(业财一体)
    • GoWind WMS(多货主仓储)
  • 商业产品

    • GoWind IM(即时通讯)
    • GoWind CRM(B2B 销售管理)
    • GoWind Trade(外贸全流程管理)
    • GoWind IoT(物联网)
    • GoWind Edu(AI 教育)
    • GoWind MES(制造执行)
    • GoWind CDC(数据管道)
    • GoWind SDP(零信任网关)
    • GoWind Quant(量化研究)
    • GoWind Trade Radar(外贸选品雷达)
    • GoWind Game Radar(游戏立项雷达)
  • 简体中文
  • English
GitHub
  • 介绍

    • GoWind 框架
    • 框架整体架构
  • go-wind 核心

    • go-wind 核心框架
    • App 生命周期管理
    • Context 传播
    • Transport 抽象
    • Log 门面接口
  • go-wind-plugins 插件

    • go-wind-plugins 插件总览
    • 插件配置系统
    • 插件注册机制(SPI)
    • 日志适配插件
    • 传输协议插件
    • 消息中间件插件
    • 编码解码插件
    • 安全与认证插件
    • 链路追踪插件
    • 缓存插件
    • 对象存储插件(OSS)
    • 限流插件
    • 指标监控插件
    • AI 插件
    • 工作流插件
    • 数据库与缓存插件
    • Bootstrap 集成与网关插件
    • 压缩与序列化插件
    • 模板渲染与验证插件
  • go-wind-bootstrap 启动器

    • go-wind-bootstrap 声明式启动器
    • Bootstrap 配置系统
    • Bootstrap SPI 机制
    • 声明式中间件编排
    • Bootstrap 命令行集成
    • Bootstrap 实战示例
  • go-wind-flow 流程引擎

    • GoWind Flow 流程引擎
  • 教程

    • 快速入门教程
    • 自定义插件开发教程
    • 多协议同时监听教程
    • 框架迁移指南

App 生命周期管理

App 是 go-wind 的引擎核心,负责管理所有 Server 的启动、运行和优雅停止。

一、生命周期阶段

二、启动流程

Run(ctx) 基于 errgroup 编排,真实执行顺序(伪代码):

func (a *App) Run(ctx context.Context) error {
    // Phase 0: beforeStart 钩子 —— 同步执行,任一 Server 启动之前;
    //          返回错误则中止启动
    runHooks(a.opts.beforeStart)

    // Phase 1: 所有 Server 通过 errgroup 并发启动
    eg, egCtx := errgroup.WithContext(ctx)
    for _, srv := range a.servers {
        eg.Go(func() error { return srv.Start(egCtx) })
        // 任一 Server 崩溃/退出 → errgroup 取消 egCtx
    }

    // Phase 1.5: afterStart 钩子 —— 所有 Server 启动完成后同步执行;
    //            返回错误则触发优雅停机
    runHooks(a.opts.afterStart)

    // 等待:信号到达 / ctx 取消 / Server 崩溃
    <-egCtx.Done()

    // Phase 2: beforeStop 钩子 —— 同步执行,优先于 Server.Stop
    runHooks(a.opts.beforeStop)

    // Phase 3: 所有 Server 并发 Stop,各自持有独立超时上下文
    stopAll()

    // Phase 4: afterStop 钩子 —— 同步执行,后于 Server.Stop
    runHooks(a.opts.afterStop)

    return runErr
}

三、启动阶段扩展点(beforeStart / afterStart)

go-wind 提供 beforeStart / afterStart 两个生命周期钩子,作为应用启动前后的扩展点,通过选项注册:

app := wind.New(
    wind.WithBeforeStart(func(ctx context.Context) error {
        // 任一 Server 启动之前执行:初始化连接池、预热缓存等
        return nil
    }),
    wind.WithAfterStart(func(ctx context.Context) error {
        // 所有 Server 启动完成之后执行:以真实监听端点注册服务、就绪探针打点等
        return nil
    }),
)

失败语义:

  • beforeStart 返回错误 → 启动中止,App 直接返回错误;
  • afterStart 返回错误 → 触发全量优雅停机。

这两个钩子是 Server 的 Start/Stop 之外、面向应用整体启动阶段的扩展点,与 WithBeforeStop / WithAfterStop 停机钩子、信号处理、级联崩溃等机制共同构成 App 的完整生命周期事件集。

四、优雅停止

4.1 并发停止

收到停机信号后,所有 Server 并发执行 Stop,无需依赖注册顺序:

Phase 2: beforeStop 钩子(同步,优先于 Stop)
Phase 3: srv1.Stop / srv2.Stop / srv3.Stop  ← 并发,各自独立超时上下文
Phase 4: afterStop 钩子(同步,后于 Stop)

设计要点:Stop 的 context 从 context.Background() 派生而不是从运行 context 派生,确保停机超时窗口真实有效;App.Stop() 只触发取消信号,不直接调用 Server.Stop(),停机逻辑统一收口。

4.2 超时控制

app := wind.New(
    wind.WithStopTimeout(30 * time.Second),
)

// 停止时,每个 Server 有 30 秒完成清理

五、信号处理

App 默认自动监听 SIGTERM / SIGINT / SIGQUIT,收到信号后触发优雅停止,适用于 Docker/K8s 环境的容器生命周期管理。需要自定义时使用 WithSignal:

app := wind.New(
    wind.WithSignal(syscall.SIGTERM), // 覆盖默认信号集
)

六、级联崩溃

任何一个 Server 发生致命错误返回时,errgroup 自动取消运行 context,其余 Server 优雅停止:

七、多 Server 示例

func main() {
    app := wind.New(
        wind.WithName("multi-server"),
        wind.WithServer(httpServer, grpcServer, wsServer),
        wind.WithStopTimeout(15 * time.Second),
    )

    if err := app.Run(context.Background()); err != nil {
        log.Fatal(err)
    }
}

相关文档

  • 核心框架介绍
  • Context 传播
  • Transport 抽象
  • 声明式启动器介绍
Edit this page
最近更新:: 2026/11/3 12:42
Contributors: Bobo
Prev
go-wind 核心框架
Next
Context 传播