Documentation
¶
Index ¶
- Variables
- func Register(typ Type, f FactoryFunc) error
- func SetLogger(l Logger)
- func Unregister(typ Type) bool
- type AutoGrowEnginePool
- func (p *AutoGrowEnginePool) Acquire() (Engine, error)
- func (p *AutoGrowEnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
- func (p *AutoGrowEnginePool) ClearError()
- func (p *AutoGrowEnginePool) Close() error
- func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)
- func (p *AutoGrowEnginePool) ExecuteFromKey(ctx context.Context, key string) (any, error)
- func (p *AutoGrowEnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
- func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
- func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
- func (p *AutoGrowEnginePool) GetLastError() error
- func (p *AutoGrowEnginePool) GetSource() source.Reader
- func (p *AutoGrowEnginePool) Load(ctx context.Context, key string) error
- func (p *AutoGrowEnginePool) LoadMulti(ctx context.Context, keys []string) error
- func (p *AutoGrowEnginePool) LoadString(ctx context.Context, name string, code string) error
- func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error
- func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error
- func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error
- func (p *AutoGrowEnginePool) Release(e Engine)
- func (p *AutoGrowEnginePool) SetSource(source source.Reader)
- func (p *AutoGrowEnginePool) StartWatch(ctx context.Context, key string) error
- func (p *AutoGrowEnginePool) StopWatch(key string) error
- type Engine
- type EngineFactoryFunc
- type EnginePool
- func (p *EnginePool) Acquire() (Engine, error)
- func (p *EnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
- func (p *EnginePool) ClearError()
- func (p *EnginePool) Close() error
- func (p *EnginePool) Execute(ctx context.Context) (any, error)
- func (p *EnginePool) ExecuteFromKey(ctx context.Context, key string) (any, error)
- func (p *EnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
- func (p *EnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
- func (p *EnginePool) GetGlobal(name string) (any, error)
- func (p *EnginePool) GetLastError() error
- func (p *EnginePool) GetSource() source.Reader
- func (p *EnginePool) InitAll(ctx context.Context) error
- func (p *EnginePool) IsClosed() bool
- func (p *EnginePool) Load(ctx context.Context, key string) error
- func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error
- func (p *EnginePool) LoadString(ctx context.Context, name string, code string) error
- func (p *EnginePool) RegisterFunction(name string, fn any) error
- func (p *EnginePool) RegisterGlobal(name string, value any) error
- func (p *EnginePool) RegisterModule(name string, module any) error
- func (p *EnginePool) Release(e Engine)
- func (p *EnginePool) SetSource(source source.Reader)
- func (p *EnginePool) StartWatch(ctx context.Context, key string) error
- func (p *EnginePool) StopWatch(key string) error
- type FactoryFunc
- type FunctionRegistrar
- type GlobalAccessor
- type Logger
- type Manager
- func (m *Manager) CloseAll() error
- func (m *Manager) Get(name string) (Engine, bool)
- func (m *Manager) GetDefault() (Engine, bool)
- func (m *Manager) InitAll(ctx context.Context) error
- func (m *Manager) Register(name string, eng Engine) error
- func (m *Manager) Remove(name string, closeIfExists bool)
- func (m *Manager) SetDefault(name string)
- type ModuleRegistrar
- type Quota
- type QuotaController
- type RuntimeHook
- type RuntimeHookRegistrar
- type SandboxConfigurator
- type ScriptEngine
- type ScriptExecutor
- type ScriptLoader
- type ScriptWatcher
- type SlogLogger
- func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) With(args ...any) Logger
- type SyncExecutor
- type Type
Constants ¶
This section is empty.
Variables ¶
var ErrCapabilityNotSupported = errors.New("script engine: capability not supported by this engine type")
ErrCapabilityNotSupported 表示引擎未实现调用方请求的可选能力。 调用方收到此错误后应按能力降级处理,而不是将其视为致命故障。
var ErrQuotaExceeded = errors.New("script engine: execution quota exceeded")
ErrQuotaExceeded 表示脚本超过配置的时间或指令预算并被中断。
Functions ¶
Types ¶
type AutoGrowEnginePool ¶
type AutoGrowEnginePool struct {
// contains filtered or unexported fields
}
AutoGrowEnginePool 管理可按需扩容的 Engine 实例。 优先复用空闲实例,没有空闲实例且未达到上限时创建新实例。
func NewAutoGrowEnginePool ¶
func NewAutoGrowEnginePool(initialSize, maxSize int, typ Type) (*AutoGrowEnginePool, error)
NewAutoGrowEnginePool 使用 typ 对应的注册工厂创建自动扩容池。 工厂会自动执行每个实例的 Init。
func NewAutoGrowEnginePoolWithFactory ¶
func NewAutoGrowEnginePoolWithFactory(initialSize, maxSize int, factory EngineFactoryFunc) (*AutoGrowEnginePool, error)
NewAutoGrowEnginePoolWithFactory 使用自定义工厂创建自动扩容池。 initialSize 是预创建数量,maxSize 是实例总数上限。
func (*AutoGrowEnginePool) Acquire ¶
func (p *AutoGrowEnginePool) Acquire() (Engine, error)
Acquire 借出一个实例;达到上限且没有空闲实例时阻塞等待。
func (*AutoGrowEnginePool) CallFunction ¶
func (p *AutoGrowEnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
RegisterModule 注册脚本模块。
func (*AutoGrowEnginePool) ClearError ¶
func (p *AutoGrowEnginePool) ClearError()
func (*AutoGrowEnginePool) Close ¶
func (p *AutoGrowEnginePool) Close() error
Close 关闭池并释放所有空闲实例;借出中的实例在归还时关闭。
func (*AutoGrowEnginePool) Execute ¶
func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)
ExecuteFromKey 加载并执行 key 对应脚本。
func (*AutoGrowEnginePool) ExecuteFromKey ¶
ExecuteFromKeys 批量加载并执行脚本。
func (*AutoGrowEnginePool) ExecuteFromKeys ¶
ExecuteString 编译并执行内联脚本。
func (*AutoGrowEnginePool) ExecuteString ¶
func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
RegisterGlobal 注册全局变量。
func (*AutoGrowEnginePool) GetGlobal ¶
func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
RegisterFunction 注册宿主函数。
func (*AutoGrowEnginePool) GetLastError ¶
func (p *AutoGrowEnginePool) GetLastError() error
ClearError 清除最近一次错误。
func (*AutoGrowEnginePool) GetSource ¶
func (p *AutoGrowEnginePool) GetSource() source.Reader
Load 从绑定源加载脚本。
func (*AutoGrowEnginePool) Load ¶
func (p *AutoGrowEnginePool) Load(ctx context.Context, key string) error
LoadMulti 按顺序从绑定源加载多个脚本。
func (*AutoGrowEnginePool) LoadMulti ¶
func (p *AutoGrowEnginePool) LoadMulti(ctx context.Context, keys []string) error
LoadString 编译不经过绑定源的内联脚本。
func (*AutoGrowEnginePool) LoadString ¶
Execute 执行借出实例中已加载的脚本。
func (*AutoGrowEnginePool) RegisterFunction ¶
func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error
CallFunction 调用脚本函数。
func (*AutoGrowEnginePool) RegisterGlobal ¶
func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error
GetGlobal 读取全局变量。
func (*AutoGrowEnginePool) RegisterModule ¶
func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error
StartWatch 开始监听脚本变化。
func (*AutoGrowEnginePool) Release ¶
func (p *AutoGrowEnginePool) Release(e Engine)
Release 将实例归还池中;池已关闭或通道已满时关闭实例。
func (*AutoGrowEnginePool) SetSource ¶
func (p *AutoGrowEnginePool) SetSource(source source.Reader)
GetSource 返回借出实例当前绑定的脚本源。
func (*AutoGrowEnginePool) StartWatch ¶
func (p *AutoGrowEnginePool) StartWatch(ctx context.Context, key string) error
StopWatch 停止监听脚本变化。
func (*AutoGrowEnginePool) StopWatch ¶
func (p *AutoGrowEnginePool) StopWatch(key string) error
GetLastError 返回最近一次错误。
type Engine ¶
type Engine interface {
ScriptEngine
ScriptLoader
ScriptExecutor
GlobalAccessor
FunctionRegistrar
ModuleRegistrar
ScriptWatcher
}
Engine 聚合所有完整引擎能力。调用方不应假设轻量引擎一定实现此接口, 应使用 AsLoader、AsExecutor 等辅助函数或类型断言检查具体能力。
func NewScriptEngine ¶
NewScriptEngine 使用 typ 对应的已注册工厂创建引擎实例。
type EngineFactoryFunc ¶
EngineFactoryFunc 创建一个已经完成初始化的 Engine 实例。 工厂可在 Init 前完成沙箱白名单、全局变量等实例级配置。
type EnginePool ¶
type EnginePool struct {
// contains filtered or unexported fields
}
EnginePool 管理固定数量的独立 Engine 实例,适合需要稳定并发容量的场景。
func NewEnginePool ¶
func NewEnginePool(size int, typ Type) (*EnginePool, error)
NewEnginePool 创建并初始化包含 size 个实例的引擎池。
func (*EnginePool) Acquire ¶
func (p *EnginePool) Acquire() (Engine, error)
Acquire 借出一个引擎;没有空闲实例时阻塞等待,池已关闭时返回错误。
func (*EnginePool) CallFunction ¶
CallFunction 调用脚本函数。
func (*EnginePool) Execute ¶
func (p *EnginePool) Execute(ctx context.Context) (any, error)
Execute 执行借出实例中已加载的脚本。
func (*EnginePool) ExecuteFromKey ¶
ExecuteFromKey 加载并立即执行 key 对应脚本。
func (*EnginePool) ExecuteFromKeys ¶
ExecuteFromKeys 批量加载并执行多个脚本。
func (*EnginePool) ExecuteString ¶
ExecuteString 编译并立即执行内联脚本。
func (*EnginePool) GetGlobal ¶
func (p *EnginePool) GetGlobal(name string) (any, error)
GetGlobal 读取全局变量。
func (*EnginePool) GetSource ¶
func (p *EnginePool) GetSource() source.Reader
GetSource 返回借出实例绑定的脚本源。
func (*EnginePool) InitAll ¶
func (p *EnginePool) InitAll(ctx context.Context) error
InitAll 重新初始化池中所有实例。
func (*EnginePool) Load ¶
func (p *EnginePool) Load(ctx context.Context, key string) error
Load 从绑定源加载一个脚本。
func (*EnginePool) LoadMulti ¶
func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error
LoadMulti 从绑定源按顺序加载多个脚本。
func (*EnginePool) LoadString ¶
LoadString 编译一个不经过绑定源的内联脚本。
func (*EnginePool) RegisterFunction ¶
func (p *EnginePool) RegisterFunction(name string, fn any) error
RegisterFunction 注册宿主函数。
func (*EnginePool) RegisterGlobal ¶
func (p *EnginePool) RegisterGlobal(name string, value any) error
RegisterGlobal 注册全局变量。
func (*EnginePool) RegisterModule ¶
func (p *EnginePool) RegisterModule(name string, module any) error
RegisterModule 注册脚本模块。
func (*EnginePool) Release ¶
func (p *EnginePool) Release(e Engine)
Release 将引擎归还池中;池已关闭或通道已满时关闭该实例。
func (*EnginePool) SetSource ¶
func (p *EnginePool) SetSource(source source.Reader)
SetSource 为借出的实例绑定脚本源。
func (*EnginePool) StartWatch ¶
func (p *EnginePool) StartWatch(ctx context.Context, key string) error
StartWatch 开始监听脚本变化。
type FactoryFunc ¶
FactoryFunc 定义创建 Engine 实例的工厂函数签名。
type FunctionRegistrar ¶
type FunctionRegistrar interface {
// RegisterFunction 注册脚本可调用的 Go 函数;函数签名由具体引擎校验。
RegisterFunction(name string, fn any) error
// CallFunction 调用脚本函数并返回结果;ctx 可用于取消或超时控制。
CallFunction(ctx context.Context, name string, args ...any) (any, error)
}
FunctionRegistrar 提供宿主函数注册和脚本函数调用能力。
func AsFunctionRegistrar ¶
func AsFunctionRegistrar(e any) FunctionRegistrar
AsFunctionRegistrar 返回 e 的 FunctionRegistrar 能力;不支持时返回 nil。
type GlobalAccessor ¶
type GlobalAccessor interface {
// RegisterGlobal 注册或覆盖脚本可见的全局变量。
RegisterGlobal(name string, value any) error
// GetGlobal 读取脚本全局变量;变量不存在时返回错误。
GetGlobal(name string) (any, error)
}
GlobalAccessor 提供脚本全局变量的注册和读取能力。
func AsGlobalAccessor ¶
func AsGlobalAccessor(e any) GlobalAccessor
AsGlobalAccessor 返回 e 的 GlobalAccessor 能力;不支持时返回 nil。
type Logger ¶
type Logger interface {
// Debug 记录 DEBUG 级别日志。
Debug(ctx context.Context, msg string, args ...any)
// Info 记录 INFO 级别日志。
Info(ctx context.Context, msg string, args ...any)
// Warn 记录 WARN 级别日志。
Warn(ctx context.Context, msg string, args ...any)
// Error 记录 ERROR 级别日志。
Error(ctx context.Context, msg string, args ...any)
// With 返回附带结构化字段的新日志器。
With(args ...any) Logger
}
Logger 是框架内部使用的最小日志接口,便于适配 slog、zap、zerolog 等后端。
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager 按名称管理多个引擎实例,并统一控制初始化、关闭和默认实例。
func (*Manager) SetDefault ¶
SetDefault 设置 GetDefault 使用的实例名称。
type ModuleRegistrar ¶
type ModuleRegistrar interface {
// RegisterModule 以 name 注册模块,供脚本导入或访问。
RegisterModule(name string, module any) error
}
ModuleRegistrar 为支持模块系统的引擎注册模块,例如 Lua 的 require 模块。 module 的具体结构由引擎实现决定。
func AsModuleRegistrar ¶
func AsModuleRegistrar(e any) ModuleRegistrar
AsModuleRegistrar 返回 e 的 ModuleRegistrar 能力;不支持时返回 nil。
type Quota ¶
type Quota struct {
// Timeout 定义墙上时钟超时时间。
Timeout time.Duration
// MaxInstructions 定义允许执行的最大指令数;零值表示不限制。
MaxInstructions int
}
Quota 定义一次同步执行的时间和指令数预算;两个字段都为零表示不限制。
type QuotaController ¶
type QuotaController interface {
// SetQuota 设置执行预算;传入零值会取消预算限制。
SetQuota(q Quota)
}
QuotaController 为后续 ExecuteSync 调用设置执行预算。
func AsQuotaController ¶
func AsQuotaController(e any) QuotaController
AsQuotaController 返回 e 的 QuotaController 能力;不支持时返回 nil。
type RuntimeHook ¶
RuntimeHook 在运行时准备完成后执行。使用状态池的引擎会在每次取得状态时重放钩子。 它允许调用方向运行时注入业务模块、宿主函数和反向回调。使用运行时池的引擎必须在每次重新取得状态时重放所有钩子, 并先清理上一个实例注入的业务全局变量,以保证引擎实例之间相互隔离。
type RuntimeHookRegistrar ¶
type RuntimeHookRegistrar interface {
// AddRuntimeHook 注册一个运行时钩子。
AddRuntimeHook(hook RuntimeHook) error
}
RuntimeHookRegistrar 是支持注册运行时钩子的可选接口。 Init 前注册的钩子延迟到初始化后执行,Init 后注册的钩子立即执行。
func AsRuntimeHookRegistrar ¶
func AsRuntimeHookRegistrar(e any) RuntimeHookRegistrar
AsRuntimeHookRegistrar 返回 e 的 RuntimeHookRegistrar 能力;不支持时返回 nil。
type SandboxConfigurator ¶
type SandboxConfigurator interface {
// SetOpenLibs 设置允许打开的标准库;必须在 Init 前调用。
SetOpenLibs(libs ...string)
}
SandboxConfigurator 配置脚本可使用的标准库白名单。 Lua 引擎实现此能力;不支持沙箱的引擎不会实现该接口。
func AsSandboxConfigurator ¶
func AsSandboxConfigurator(e any) SandboxConfigurator
AsSandboxConfigurator 返回 e 的 SandboxConfigurator 能力;不支持时返回 nil。
type ScriptEngine ¶
type ScriptEngine interface {
// GetType 返回引擎类型标识。
GetType() Type
// Init 创建运行时并完成引擎初始化,必须在加载或执行脚本前调用。
// 初始化失败或引擎已经初始化时返回错误。
Init(ctx context.Context) error
// Close 释放运行时、脚本状态和其他底层资源。
// 关闭后再次使用前必须重新调用 Init。
Close() error
// IsInitialized 判断引擎是否已初始化且尚未关闭。
IsInitialized() bool
// GetLastError 返回引擎记录的最近一次错误;没有错误时返回 nil。
GetLastError() error
// ClearError 清除引擎记录的错误状态。
ClearError()
}
ScriptEngine 是所有脚本引擎都必须实现的生命周期接口。 轻量引擎可以只实现部分能力接口;完整引擎再组合为 Engine。
type ScriptExecutor ¶
type ScriptExecutor interface {
// Execute 按加载顺序执行队列中的脚本,并返回最后一个结果。
Execute(ctx context.Context) (any, error)
// ExecuteFromKey 从脚本源加载 key 对应脚本并立即执行。
ExecuteFromKey(ctx context.Context, key string) (any, error)
// ExecuteFromKeys 批量加载并执行多个 key,结果顺序与 keys 保持一致。
ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
// ExecuteString 编译并立即执行内联脚本,不经过绑定脚本源。
ExecuteString(ctx context.Context, name string, code string) (any, error)
}
ScriptExecutor 提供脚本执行能力。
func AsExecutor ¶
func AsExecutor(e any) ScriptExecutor
AsExecutor 返回 e 的 ScriptExecutor 能力;不支持时返回 nil。
type ScriptLoader ¶
type ScriptLoader interface {
// SetSource 绑定脚本源;传入 nil 会清除当前绑定。
// 后续 Load 和 ExecuteFromKey 调用都会通过该脚本源读取内容。
SetSource(source source.Reader)
// GetSource 返回当前绑定的脚本源;未绑定时返回 nil。
GetSource() source.Reader
// Load 根据 key 从绑定脚本源加载一个脚本并加入执行队列。
Load(ctx context.Context, key string) error
// LoadMulti 按 keys 顺序加载多个脚本,遇到第一个错误时停止。
LoadMulti(ctx context.Context, keys []string) error
// LoadString 编译直接传入的脚本,不经过绑定脚本源;name 用于错误诊断。
LoadString(ctx context.Context, name string, code string) error
}
ScriptLoader 提供与具体 IO 实现解耦的脚本加载能力。 脚本从绑定的 source.Reader 读取,支持文件、内存、远程服务和组合源。
type ScriptWatcher ¶
type ScriptWatcher interface {
// StartWatch 开始监听 key 对应脚本的变化并在变化后重新加载。
StartWatch(ctx context.Context, key string) error
// StopWatch 停止监听 key 对应脚本并释放监听协程。
StopWatch(key string) error
}
ScriptWatcher 提供脚本热更新能力。 引擎会通过绑定脚本源的 Watch 方法接收变化通知并重新加载脚本。
type SlogLogger ¶
func (SlogLogger) Debug ¶
func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)
Debug 转发到 slog.Logger.DebugContext。
func (SlogLogger) Error ¶
func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)
Error 转发到 slog.Logger.ErrorContext。
func (SlogLogger) Info ¶
func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)
Info 转发到 slog.Logger.InfoContext。
func (SlogLogger) Warn ¶
func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)
Warn 转发到 slog.Logger.WarnContext。
func (SlogLogger) With ¶
func (s SlogLogger) With(args ...any) Logger
With 返回底层日志器附带字段的新 SlogLogger。
type SyncExecutor ¶
type SyncExecutor interface {
// ExecuteSync 同步执行最近加载的脚本,超出预算时返回 ErrQuotaExceeded。
ExecuteSync(ctx context.Context) (any, error)
}
SyncExecutor 提供无额外协程和通道分配的同步执行路径,适合高频调用。
func AsSyncExecutor ¶
func AsSyncExecutor(e any) SyncExecutor
AsSyncExecutor 返回 e 的 SyncExecutor 能力;不支持时返回 nil。
type Type ¶
type Type string
Type 标识脚本引擎实现类型。
const ( UnknownType Type = "unknown" // LuaType 表示 gopher-lua 引擎。 LuaType Type = "lua" // JavaScriptType 表示 goja JavaScript 引擎。 JavaScriptType Type = "javascript" // GPythonType 表示 gpython Python 引擎。 GPythonType Type = "gpython" // YaegiType 表示 Yaegi Go 解释器引擎。 YaegiType Type = "yaegi" // WazeroType 表示 wazero WebAssembly 引擎。 WazeroType Type = "wazero" // CELType 表示 cel-go CEL 表达式引擎。 CELType Type = "cel" // ExprType 表示 expr-lang/expr 表达式引擎。 ExprType Type = "expr" // StarlarkType 表示 starlark-go 引擎。 StarlarkType Type = "starlark" // TclType 表示 modernc.org/tcl 引擎。 TclType Type = "tcl" )