163 lines
4.2 KiB
Go
163 lines
4.2 KiB
Go
package engine
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
|
|
"cellbot/internal/protocol"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// Dispatcher 事件分发器
|
|
// 管理事件处理器并按照优先级分发事件
|
|
type Dispatcher struct {
|
|
handlers []protocol.EventHandler
|
|
middlewares []protocol.Middleware
|
|
logger *zap.Logger
|
|
eventBus *EventBus
|
|
}
|
|
|
|
// NewDispatcher 创建事件分发器
|
|
func NewDispatcher(eventBus *EventBus, logger *zap.Logger) *Dispatcher {
|
|
return &Dispatcher{
|
|
handlers: make([]protocol.EventHandler, 0),
|
|
middlewares: make([]protocol.Middleware, 0),
|
|
logger: logger.Named("dispatcher"),
|
|
eventBus: eventBus,
|
|
}
|
|
}
|
|
|
|
// RegisterHandler 注册事件处理器
|
|
func (d *Dispatcher) RegisterHandler(handler protocol.EventHandler) {
|
|
d.handlers = append(d.handlers, handler)
|
|
// 按优先级排序(数值越小优先级越高)
|
|
sort.Slice(d.handlers, func(i, j int) bool {
|
|
return d.handlers[i].Priority() < d.handlers[j].Priority()
|
|
})
|
|
|
|
d.logger.Debug("Handler registered",
|
|
zap.Int("priority", handler.Priority()),
|
|
zap.Int("total_handlers", len(d.handlers)))
|
|
}
|
|
|
|
// UnregisterHandler 取消注册事件处理器
|
|
func (d *Dispatcher) UnregisterHandler(handler protocol.EventHandler) {
|
|
for i, h := range d.handlers {
|
|
if h == handler {
|
|
d.handlers = append(d.handlers[:i], d.handlers[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
d.logger.Debug("Handler unregistered",
|
|
zap.Int("total_handlers", len(d.handlers)))
|
|
}
|
|
|
|
// RegisterMiddleware 注册中间件
|
|
func (d *Dispatcher) RegisterMiddleware(middleware protocol.Middleware) {
|
|
d.middlewares = append(d.middlewares, middleware)
|
|
d.logger.Debug("Middleware registered",
|
|
zap.Int("total_middlewares", len(d.middlewares)))
|
|
}
|
|
|
|
// Start 启动分发器
|
|
func (d *Dispatcher) Start(ctx context.Context) {
|
|
// 订阅所有类型的事件
|
|
for _, eventType := range []protocol.EventType{
|
|
protocol.EventTypeMessage,
|
|
protocol.EventTypeNotice,
|
|
protocol.EventTypeRequest,
|
|
protocol.EventTypeMeta,
|
|
} {
|
|
eventChan := d.eventBus.Subscribe(eventType, nil)
|
|
go d.eventLoop(ctx, eventChan)
|
|
}
|
|
|
|
d.logger.Info("Dispatcher started")
|
|
}
|
|
|
|
// Stop 停止分发器
|
|
func (d *Dispatcher) Stop() {
|
|
d.logger.Info("Dispatcher stopped")
|
|
}
|
|
|
|
// eventLoop 事件循环
|
|
func (d *Dispatcher) eventLoop(ctx context.Context, eventChan chan protocol.Event) {
|
|
for {
|
|
select {
|
|
case event, ok := <-eventChan:
|
|
if !ok {
|
|
return
|
|
}
|
|
d.handleEvent(ctx, event)
|
|
case <-ctx.Done():
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleEvent 处理单个事件
|
|
func (d *Dispatcher) handleEvent(ctx context.Context, event protocol.Event) {
|
|
d.logger.Debug("Processing event",
|
|
zap.String("type", string(event.GetType())),
|
|
zap.String("detail_type", event.GetDetailType()))
|
|
|
|
// 通过中间件链处理事件
|
|
next := d.createHandlerChain(ctx, event)
|
|
|
|
// 执行中间件链
|
|
if len(d.middlewares) > 0 {
|
|
d.executeMiddlewares(ctx, event, func(ctx context.Context, e protocol.Event) error {
|
|
next(ctx, e)
|
|
return nil
|
|
})
|
|
} else {
|
|
next(ctx, event)
|
|
}
|
|
}
|
|
|
|
// createHandlerChain 创建处理器链
|
|
func (d *Dispatcher) createHandlerChain(ctx context.Context, event protocol.Event) func(context.Context, protocol.Event) {
|
|
return func(ctx context.Context, e protocol.Event) {
|
|
for _, handler := range d.handlers {
|
|
if handler.Match(event) {
|
|
if err := handler.Handle(ctx, e); err != nil {
|
|
d.logger.Error("Handler execution failed",
|
|
zap.Error(err),
|
|
zap.String("event_type", string(e.GetType())))
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// executeMiddlewares 执行中间件链
|
|
func (d *Dispatcher) executeMiddlewares(ctx context.Context, event protocol.Event, next func(context.Context, protocol.Event) error) {
|
|
// 从后向前构建中间件链
|
|
handler := next
|
|
for i := len(d.middlewares) - 1; i >= 0; i-- {
|
|
middleware := d.middlewares[i]
|
|
currentHandler := handler
|
|
handler = func(ctx context.Context, e protocol.Event) error {
|
|
if err := middleware.Process(ctx, e, currentHandler); err != nil {
|
|
d.logger.Error("Middleware execution failed",
|
|
zap.Error(err),
|
|
zap.String("event_type", string(e.GetType())))
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
// 执行中间件链
|
|
handler(ctx, event)
|
|
}
|
|
|
|
// GetHandlerCount 获取处理器数量
|
|
func (d *Dispatcher) GetHandlerCount() int {
|
|
return len(d.handlers)
|
|
}
|
|
|
|
// GetMiddlewareCount 获取中间件数量
|
|
func (d *Dispatcher) GetMiddlewareCount() int {
|
|
return len(d.middlewares)
|
|
}
|