runtime

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2026 License: MIT Imports: 11 Imported by: 0

README

runtime — 插件运行时

负责插件的生命周期管理:依赖解析(拓扑排序)、有序初始化与优雅关闭。

功能

  • 注册并管理所有插件
  • 基于依赖声明自动拓扑排序,确保依赖先于依赖方初始化
  • 并发安全的插件状态跟踪
  • 信号监听 + 优雅关闭(按逆序调用 Teardown

快速开始

package main

import (
    "context"
    "github.com/leeforge/framework/runtime"
)

func main() {
    rt := runtime.New(appCtx) // appCtx 实现 plugin.AppContext

    // 注册插件(顺序无关,运行时会自动拓扑排序)
    rt.Register(&AuthPlugin{})
    rt.Register(&UserPlugin{})   // 依赖 AuthPlugin
    rt.Register(&MediaPlugin{})

    // 启动:按依赖顺序调用各插件的 Setup
    if err := rt.Bootstrap(context.Background()); err != nil {
        log.Fatalf("bootstrap failed: %v", err)
    }

    // 阻塞直到收到 SIGINT / SIGTERM
    rt.Wait()

    // 优雅关闭:按逆序调用各插件的 Teardown
    rt.Shutdown(context.Background())
}

事件总线

运行时内置 EventBus,插件可通过 AppContext.EventBus() 访问:

// 在插件 A 中发布事件
app.EventBus().Publish(ctx, "order.created", orderPayload)

// 在插件 B 中订阅事件
app.EventBus().Subscribe("order.created", func(ctx context.Context, e plugin.Event) error {
    // 异步处理订单创建事件
    return nil
})

错误处理

  • Bootstrap 时任意插件的 Setup 失败,会立即返回错误,已初始化的插件会按逆序调用 Teardown
  • 循环依赖在 Bootstrap 前即被检测,返回描述性错误

注意事项

  • 不要在 Setup 内启动长时间阻塞操作,应使用 goroutine 并在 Teardown 中优雅停止
  • EventBus 的事件处理器应当幂等,避免重复消费导致副作用

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewEventBus

func NewEventBus(bufferSize int, logger *zap.Logger) *eventBus

NewEventBus creates a new EventBus with the given buffer size.

Types

type Config

type Config struct {
	Router      chi.Router
	DB          plugin.Database
	Redis       *redis.Client
	Logger      *zap.Logger
	EventBuffer int // default 1024
}

Config holds configuration for creating a new Runtime.

type Runtime

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

Runtime manages plugin lifecycle with correct dependency ordering.

func NewRuntime

func NewRuntime(cfg Config) *Runtime

NewRuntime creates a new runtime instance.

func (*Runtime) BootOrder

func (r *Runtime) BootOrder() []string

BootOrder returns the topological order used during bootstrap.

func (*Runtime) Bootstrap

func (r *Runtime) Bootstrap(ctx context.Context) error

Bootstrap initializes all plugins in dependency order.

func (*Runtime) GetPluginModels

func (r *Runtime) GetPluginModels() map[string][]any

GetPluginModels returns models registered by plugins.

func (*Runtime) GetPluginState

func (r *Runtime) GetPluginState(name string) (plugin.PluginState, bool)

GetPluginState returns the state of a plugin by name.

func (*Runtime) ListPlugins

func (r *Runtime) ListPlugins() map[string]plugin.PluginState

ListPlugins returns a snapshot of all plugin states.

func (*Runtime) Publish

func (r *Runtime) Publish(ctx context.Context, event plugin.Event) error

Publish sends an event through the event bus.

func (*Runtime) Register

func (r *Runtime) Register(p plugin.Plugin) error

Register adds a plugin. Must be called before Bootstrap.

func (*Runtime) Services

func (r *Runtime) Services() *plugin.ServiceRegistry

Services returns the plugin service registry for pre-registering core services. Must be called before Bootstrap.

func (*Runtime) Shutdown

func (r *Runtime) Shutdown(ctx context.Context) error

Shutdown disables plugins in reverse topological order.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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