sdk

package
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: May 27, 2025 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateLogger

func CreateLogger(name, level string, output string) (hclog.Logger, error)

CreateLogger 创建日志记录器 name: 日志记录器名称 level: 日志级别 output: 日志输出 返回: 日志记录器

func CreatePluginInfoFromConfig

func CreatePluginInfoFromConfig(config map[string]interface{}) (api.PluginInfo, error)

CreatePluginInfoFromConfig 从配置创建插件信息 config: 配置数据 返回: 插件信息和错误

func DisableBlockProfile

func DisableBlockProfile()

DisableBlockProfile 禁用阻塞分析

func DisableMutexProfile

func DisableMutexProfile()

DisableMutexProfile 禁用互斥锁分析

func DumpGoroutineProfile

func DumpGoroutineProfile(path string) error

DumpGoroutineProfile 导出goroutine分析

func DumpHeapProfile

func DumpHeapProfile(path string) error

DumpHeapProfile 导出堆分析

func EnableBlockProfile

func EnableBlockProfile()

EnableBlockProfile 启用阻塞分析

func EnableMutexProfile

func EnableMutexProfile()

EnableMutexProfile 启用互斥锁分析

func FormatPluginError

func FormatPluginError(pluginID, message string, err error) error

FormatPluginError 格式化插件错误 pluginID: 插件ID message: 错误消息 err: 原始错误 返回: 格式化的错误

func GetPluginConfigDir

func GetPluginConfigDir(pluginID string) (string, error)

GetPluginConfigDir 获取插件配置目录 pluginID: 插件ID 返回: 配置目录路径和错误

func GetPluginDataDir

func GetPluginDataDir(pluginID string) (string, error)

GetPluginDataDir 获取插件数据目录 pluginID: 插件ID 返回: 数据目录路径和错误

func GetPluginLogDir

func GetPluginLogDir(pluginID string) (string, error)

GetPluginLogDir 获取插件日志目录 pluginID: 插件ID 返回: 日志目录路径和错误

func LoadConfig

func LoadConfig(path string, config interface{}) error

LoadConfig 从文件加载配置 path: 配置文件路径 config: 配置对象指针 返回: 错误

func MergeConfig

func MergeConfig(target, source interface{}) error

MergeConfig 合并配置 target: 目标配置对象指针 source: 源配置对象 返回: 错误

func ParsePluginConfig

func ParsePluginConfig(configData map[string]interface{}) (api.PluginConfig, error)

ParsePluginConfig 解析插件配置 configData: 配置数据 返回: 插件配置和错误

func RetryWithBackoff

func RetryWithBackoff(ctx context.Context, fn func() error, maxRetries int, initialBackoff, maxBackoff time.Duration) error

RetryWithBackoff 带退避的重试 ctx: 上下文 fn: 要重试的函数 maxRetries: 最大重试次数 initialBackoff: 初始退避时间 maxBackoff: 最大退避时间 返回: 错误

func Run

func Run(plugin api.Plugin, config RunnerConfig) error

Run 运行插件的便捷函数

func SaveConfig

func SaveConfig(path string, config interface{}) error

SaveConfig 保存配置到文件 path: 配置文件路径 config: 配置对象 返回: 错误

func StartCPUProfile

func StartCPUProfile(path string) (*os.File, error)

StartCPUProfile 开始CPU分析

func StopCPUProfile

func StopCPUProfile(f *os.File)

StopCPUProfile 停止CPU分析

func ValidatePluginInfo

func ValidatePluginInfo(info api.PluginInfo) error

ValidatePluginInfo 验证插件信息 info: 插件信息 返回: 错误

func WaitForSignal

func WaitForSignal(ctx context.Context, signals <-chan os.Signal) os.Signal

WaitForSignal 等待信号 ctx: 上下文 signals: 信号通道 返回: 收到的信号

Types

type BasePlugin

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

BasePlugin 提供了Plugin接口的基础实现 可以被具体的插件实现嵌入以减少重复代码

func NewBasePlugin

func NewBasePlugin(info api.PluginInfo, logger hclog.Logger) *BasePlugin

NewBasePlugin 创建一个新的基础插件

func (*BasePlugin) GetConfig

func (p *BasePlugin) GetConfig() api.PluginConfig

GetConfig 获取插件配置

func (*BasePlugin) GetInfo

func (p *BasePlugin) GetInfo() api.PluginInfo

GetInfo 返回插件信息

func (*BasePlugin) GetLastError

func (p *BasePlugin) GetLastError() error

GetLastError 获取最后一个错误

func (*BasePlugin) GetLogger

func (p *BasePlugin) GetLogger() hclog.Logger

GetLogger 获取日志记录器

func (*BasePlugin) GetStartTime

func (p *BasePlugin) GetStartTime() time.Time

GetStartTime 获取启动时间

func (*BasePlugin) GetState

func (p *BasePlugin) GetState() api.PluginState

GetState 获取插件状态

func (*BasePlugin) GetStats

func (p *BasePlugin) GetStats() map[string]interface{}

GetStats 获取统计信息

func (*BasePlugin) GetStopTime

func (p *BasePlugin) GetStopTime() time.Time

GetStopTime 获取停止时间

func (*BasePlugin) HealthCheck

func (p *BasePlugin) HealthCheck(ctx context.Context) (api.HealthStatus, error)

HealthCheck 执行健康检查

func (*BasePlugin) IncrementStat

func (p *BasePlugin) IncrementStat(key string, delta int64)

IncrementStat 增加统计计数

func (*BasePlugin) Init

func (p *BasePlugin) Init(ctx context.Context, config api.PluginConfig) error

Init 初始化插件

func (*BasePlugin) SetConfig

func (p *BasePlugin) SetConfig(config api.PluginConfig)

SetConfig 设置插件配置

func (*BasePlugin) SetLastError

func (p *BasePlugin) SetLastError(err error)

SetLastError 设置最后一个错误

func (*BasePlugin) SetLogger

func (p *BasePlugin) SetLogger(logger hclog.Logger)

SetLogger 设置日志记录器

func (*BasePlugin) SetStat

func (p *BasePlugin) SetStat(key string, value interface{})

SetStat 设置统计信息

func (*BasePlugin) SetState

func (p *BasePlugin) SetState(state api.PluginState)

SetState 设置插件状态

func (*BasePlugin) Start

func (p *BasePlugin) Start(ctx context.Context) error

Start 启动插件

func (*BasePlugin) Stop

func (p *BasePlugin) Stop(ctx context.Context) error

Stop 停止插件

type Communication

type Communication interface {
	// RegisterService 注册服务
	// service: 服务实例
	// 返回: 错误
	RegisterService(service interface{}) error

	// GetService 获取服务
	// name: 服务名称
	// 返回: 服务实例和错误
	GetService(name string) (interface{}, error)

	// SendMessage 发送消息
	// target: 目标
	// message: 消息内容
	// 返回: 错误
	SendMessage(target string, message interface{}) error

	// ReceiveMessage 接收消息
	// 返回: 源、消息内容和错误
	ReceiveMessage() (string, interface{}, error)

	// Subscribe 订阅主题
	// topic: 主题
	// handler: 处理函数
	// 返回: 错误
	Subscribe(topic string, handler func(message interface{})) error

	// Publish 发布消息到主题
	// topic: 主题
	// message: 消息内容
	// 返回: 错误
	Publish(topic string, message interface{}) error

	// Close 关闭通信
	// 返回: 错误
	Close() error
}

Communication 定义了通信接口 提供了插件与主框架之间以及插件之间的通信能力

type CommunicationFactory

type CommunicationFactory interface {
	// CreateCommunication 创建通信实例
	// protocol: 通信协议
	// options: 选项
	// 返回: 通信实例和错误
	CreateCommunication(protocol CommunicationProtocol, options map[string]interface{}) (Communication, error)
}

CommunicationFactory 定义了通信工厂 用于创建不同协议的通信实例

type CommunicationHandler

type CommunicationHandler func(options map[string]interface{}) (Communication, error)

CommunicationHandler 定义了通信处理器

type CommunicationProtocol

type CommunicationProtocol string

CommunicationProtocol 定义了通信协议

const (
	ProtocolGRPC      CommunicationProtocol = "grpc"      // gRPC协议
	ProtocolHTTP      CommunicationProtocol = "http"      // HTTP协议
	ProtocolWebSocket CommunicationProtocol = "websocket" // WebSocket协议
	ProtocolInProcess CommunicationProtocol = "inprocess" // 进程内通信
)

预定义的通信协议

type ConfigManager

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

ConfigManager 配置管理器 负责管理插件配置

func NewConfigManager

func NewConfigManager(pluginID string, logger hclog.Logger, options ...ConfigOption) (*ConfigManager, error)

NewConfigManager 创建一个新的配置管理器

func (*ConfigManager) Get

func (cm *ConfigManager) Get(key string) interface{}

Get 获取配置值

func (*ConfigManager) GetBool

func (cm *ConfigManager) GetBool(key string) bool

GetBool 获取布尔配置值

func (*ConfigManager) GetConfigDir

func (cm *ConfigManager) GetConfigDir() string

GetConfigDir 获取配置目录

func (*ConfigManager) GetConfigFile

func (cm *ConfigManager) GetConfigFile() string

GetConfigFile 获取配置文件

func (*ConfigManager) GetConfigPath

func (cm *ConfigManager) GetConfigPath() string

GetConfigPath 获取配置文件路径

func (*ConfigManager) GetData

func (cm *ConfigManager) GetData() map[string]interface{}

GetData 获取配置数据

func (*ConfigManager) GetFloat

func (cm *ConfigManager) GetFloat(key string) float64

GetFloat 获取浮点数配置值

func (*ConfigManager) GetInt

func (cm *ConfigManager) GetInt(key string) int

GetInt 获取整数配置值

func (*ConfigManager) GetPluginConfig

func (cm *ConfigManager) GetPluginConfig() (api.PluginConfig, error)

GetPluginConfig 获取插件配置

func (*ConfigManager) GetString

func (cm *ConfigManager) GetString(key string) string

GetString 获取字符串配置值

func (*ConfigManager) GetStringMap

func (cm *ConfigManager) GetStringMap(key string) map[string]string

GetStringMap 获取字符串映射配置值

func (*ConfigManager) GetStringSlice

func (cm *ConfigManager) GetStringSlice(key string) []string

GetStringSlice 获取字符串切片配置值

func (*ConfigManager) GetStruct

func (cm *ConfigManager) GetStruct(key string, result interface{}) error

GetStruct 获取结构体配置值

func (*ConfigManager) Load

func (cm *ConfigManager) Load() error

Load 加载配置

func (*ConfigManager) Save

func (cm *ConfigManager) Save() error

Save 保存配置

func (*ConfigManager) Set

func (cm *ConfigManager) Set(key string, value interface{})

Set 设置配置值

func (*ConfigManager) SetData

func (cm *ConfigManager) SetData(data map[string]interface{})

SetData 设置配置数据

func (*ConfigManager) SetPluginConfig

func (cm *ConfigManager) SetPluginConfig(config api.PluginConfig)

SetPluginConfig 设置插件配置

type ConfigOption

type ConfigOption func(*ConfigManager)

ConfigOption 配置选项

func WithConfigDir

func WithConfigDir(dir string) ConfigOption

WithConfigDir 设置配置目录

func WithConfigFile

func WithConfigFile(file string) ConfigOption

WithConfigFile 设置配置文件名

func WithEnvPrefix

func WithEnvPrefix(prefix string) ConfigOption

WithEnvPrefix 设置环境变量前缀

type DebugOption

type DebugOption func(*DebugServer)

DebugOption 调试选项

func WithDebugEnabled

func WithDebugEnabled(enabled bool) DebugOption

WithDebugEnabled 设置是否启用调试

func WithDebugPort

func WithDebugPort(port int) DebugOption

WithDebugPort 设置调试端口

type DebugServer

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

DebugServer 调试服务器 提供插件调试功能

func NewDebugServer

func NewDebugServer(pluginID string, logger hclog.Logger, options ...DebugOption) *DebugServer

NewDebugServer 创建一个新的调试服务器

func (*DebugServer) RegisterHandler

func (ds *DebugServer) RegisterHandler(path string, handler http.HandlerFunc)

RegisterHandler 注册处理器

func (*DebugServer) Start

func (ds *DebugServer) Start() error

Start 启动调试服务器

func (*DebugServer) Stop

func (ds *DebugServer) Stop() error

Stop 停止调试服务器

type DefaultCommunicationFactory

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

DefaultCommunicationFactory 默认通信工厂实现

func NewCommunicationFactory

func NewCommunicationFactory() *DefaultCommunicationFactory

NewCommunicationFactory 创建一个新的通信工厂

func (*DefaultCommunicationFactory) CreateCommunication

func (f *DefaultCommunicationFactory) CreateCommunication(protocol CommunicationProtocol, options map[string]interface{}) (Communication, error)

CreateCommunication 创建通信实例

func (*DefaultCommunicationFactory) RegisterHandler

func (f *DefaultCommunicationFactory) RegisterHandler(protocol CommunicationProtocol, handler CommunicationHandler)

RegisterHandler 注册协议处理器

type GRPCPluginAdapter

type GRPCPluginAdapter struct {
	plugin.Plugin
	Impl api.Plugin
}

GRPCPluginAdapter gRPC插件适配器 用于将我们的插件接口适配到Hashicorp的插件接口

func (*GRPCPluginAdapter) GRPCClient

func (p *GRPCPluginAdapter) GRPCClient(ctx context.Context, broker *plugin.GRPCBroker, c interface{}) (interface{}, error)

GRPCClient 实现Plugin接口

func (*GRPCPluginAdapter) GRPCServer

func (p *GRPCPluginAdapter) GRPCServer(broker *plugin.GRPCBroker, s interface{}) error

GRPCServer 实现Plugin接口

type HealthCheckFunc

type HealthCheckFunc func(ctx context.Context) (api.HealthStatus, error)

HealthCheckFunc 定义了健康检查函数类型

type InProcessCommunication

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

InProcessCommunication 进程内通信实现

func (*InProcessCommunication) Close

func (c *InProcessCommunication) Close() error

Close 关闭通信

func (*InProcessCommunication) GetService

func (c *InProcessCommunication) GetService(name string) (interface{}, error)

GetService 获取服务

func (*InProcessCommunication) Publish

func (c *InProcessCommunication) Publish(topic string, message interface{}) error

Publish 发布消息到主题

func (*InProcessCommunication) ReceiveMessage

func (c *InProcessCommunication) ReceiveMessage() (string, interface{}, error)

ReceiveMessage 接收消息

func (*InProcessCommunication) RegisterService

func (c *InProcessCommunication) RegisterService(service interface{}) error

RegisterService 注册服务

func (*InProcessCommunication) SendMessage

func (c *InProcessCommunication) SendMessage(target string, message interface{}) error

SendMessage 发送消息

func (*InProcessCommunication) Subscribe

func (c *InProcessCommunication) Subscribe(topic string, handler func(message interface{})) error

Subscribe 订阅主题

type InitFunc

type InitFunc func(ctx context.Context, config api.PluginConfig) error

InitFunc 定义了初始化函数类型

type Message

type Message struct {
	// 源
	Source string

	// 目标
	Target string

	// 内容
	Content interface{}
}

Message 消息

type PluginBuilder

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

PluginBuilder 用于构建插件 提供了流式API,简化插件的创建过程

func NewPluginBuilder

func NewPluginBuilder(id string) *PluginBuilder

NewPluginBuilder 创建一个新的插件构建器

func (*PluginBuilder) Build

func (b *PluginBuilder) Build() api.Plugin

Build 构建插件

func (*PluginBuilder) WithAuthor

func (b *PluginBuilder) WithAuthor(author string) *PluginBuilder

WithAuthor 设置插件作者

func (*PluginBuilder) WithCapability

func (b *PluginBuilder) WithCapability(capability string) *PluginBuilder

WithCapability 添加插件能力

func (*PluginBuilder) WithDependency

func (b *PluginBuilder) WithDependency(id, version string, optional bool) *PluginBuilder

WithDependency 添加插件依赖

func (*PluginBuilder) WithDescription

func (b *PluginBuilder) WithDescription(description string) *PluginBuilder

WithDescription 设置插件描述

func (*PluginBuilder) WithHealthCheckFunc

func (b *PluginBuilder) WithHealthCheckFunc(healthCheckFunc HealthCheckFunc) *PluginBuilder

WithHealthCheckFunc 设置健康检查函数

func (*PluginBuilder) WithInitFunc

func (b *PluginBuilder) WithInitFunc(initFunc InitFunc) *PluginBuilder

WithInitFunc 设置初始化函数

func (*PluginBuilder) WithLicense

func (b *PluginBuilder) WithLicense(license string) *PluginBuilder

WithLicense 设置插件许可证

func (*PluginBuilder) WithLogger

func (b *PluginBuilder) WithLogger(logger hclog.Logger) *PluginBuilder

WithLogger 设置日志记录器

func (*PluginBuilder) WithName

func (b *PluginBuilder) WithName(name string) *PluginBuilder

WithName 设置插件名称

func (*PluginBuilder) WithStartFunc

func (b *PluginBuilder) WithStartFunc(startFunc StartFunc) *PluginBuilder

WithStartFunc 设置启动函数

func (*PluginBuilder) WithStopFunc

func (b *PluginBuilder) WithStopFunc(stopFunc StopFunc) *PluginBuilder

WithStopFunc 设置停止函数

func (*PluginBuilder) WithTag

func (b *PluginBuilder) WithTag(tag string) *PluginBuilder

WithTag 添加插件标签

func (*PluginBuilder) WithVersion

func (b *PluginBuilder) WithVersion(version string) *PluginBuilder

WithVersion 设置插件版本

type PluginRunner

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

PluginRunner 用于运行插件 处理插件的启动、停止和生命周期管理

func NewPluginRunner

func NewPluginRunner(plugin api.Plugin, config RunnerConfig) *PluginRunner

NewPluginRunner 创建一个新的插件运行器

func (*PluginRunner) Run

func (r *PluginRunner) Run() error

Run 运行插件

type RunnerConfig

type RunnerConfig struct {
	// 插件ID
	PluginID string

	// 通信协议
	Protocol CommunicationProtocol

	// 通信选项
	CommOptions map[string]interface{}

	// 日志级别
	LogLevel string

	// 日志文件
	LogFile string

	// 优雅关闭超时
	ShutdownTimeout time.Duration

	// 健康检查间隔
	HealthCheckInterval time.Duration
}

RunnerConfig 定义了运行器配置

func DefaultRunnerConfig

func DefaultRunnerConfig() RunnerConfig

DefaultRunnerConfig 返回默认运行器配置

type StartFunc

type StartFunc func(ctx context.Context) error

StartFunc 定义了启动函数类型

type StopFunc

type StopFunc func(ctx context.Context) error

StopFunc 定义了停止函数类型

Jump to

Keyboard shortcuts

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