增强插件系统:添加插件类型支持
- 在插件接口和基本插件结构中引入插件类型(PluginType),并定义了多种预定义的插件类型。 - 更新插件管理器以支持按类型管理插件,新增按类型获取、初始化和启动插件的方法。 - 修改现有插件(如日志插件和统计插件)以实现插件类型接口,确保兼容性。 - 优化插件信息输出,包含插件类型信息。 此更新提升了插件系统的灵活性和可扩展性,便于未来添加更多插件类型。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -27,13 +28,14 @@ var Plugin = &StatsPlugin{
|
||||
"1.0.0",
|
||||
"系统运行时统计插件",
|
||||
"开发者",
|
||||
plugin.PluginTypeUtils, // 设置为工具类插件
|
||||
),
|
||||
stats: make(map[string]int64),
|
||||
tickerStop: make(chan bool),
|
||||
}
|
||||
|
||||
// Init 初始化插件
|
||||
func (p *StatsPlugin) Init(config map[string]interface{}) error {
|
||||
func (p *StatsPlugin) Init(ctx context.Context, config map[string]interface{}) error {
|
||||
p.config = config
|
||||
|
||||
// 初始化统计指标
|
||||
@@ -49,7 +51,7 @@ func (p *StatsPlugin) Init(config map[string]interface{}) error {
|
||||
}
|
||||
|
||||
// Start 启动插件
|
||||
func (p *StatsPlugin) Start() error {
|
||||
func (p *StatsPlugin) Start(ctx context.Context) error {
|
||||
p.startTime = time.Now()
|
||||
|
||||
// 启动定时统计任务
|
||||
@@ -70,6 +72,9 @@ func (p *StatsPlugin) Start() error {
|
||||
case <-p.tickerStop:
|
||||
p.ticker.Stop()
|
||||
return
|
||||
case <-ctx.Done():
|
||||
p.ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -79,7 +84,7 @@ func (p *StatsPlugin) Start() error {
|
||||
}
|
||||
|
||||
// Stop 停止插件
|
||||
func (p *StatsPlugin) Stop() error {
|
||||
func (p *StatsPlugin) Stop(ctx context.Context) error {
|
||||
if p.ticker != nil {
|
||||
p.tickerStop <- true
|
||||
}
|
||||
|
Reference in New Issue
Block a user