Files
Cyrene/backend/ai-core/internal/bus/conversation_bus.go
T
AskaEth 71f0a1abdb feat: Go模块路径迁移 + Docker生产部署适配 + ethend Docker兼容
- 所有Go模块路径从 github.com/yourname/cyrene-ai 迁移到 git.yeij.top/AskaEth/Cyrene
- 5个Go Dockerfile添加 GOPROXY=https://goproxy.cn,direct 解决国内构建问题
- ai-core go.mod 添加 pkg/plugins replace 指令
- Caddyfile 简化为 http:// 通配 + handle 保留 /api 前缀
- ethend Dockerfile 适配 (npm install + 仅 COPY package.json)
- ethend 新增 RUNNING_IN_DOCKER 环境变量,健康检查改用Docker服务名
- ethend 数据库状态检查支持Docker hostname (postgres/redis/qdrant/minio)
- process-manager 新增 CONTAINER_SVC_MAP + Docker模式自动检测
- 统一 docker-compose.dev.db.yml 卷名 (pg_data/redis_data/qdrant_data/minio_data)
- docker-compose.yml ethend服务挂载docker.sock + 端口变量化
- 清理 .env 统一后的残留文件与提示信息

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-30 13:43:22 +08:00

102 lines
2.3 KiB
Go

package bus
import (
"git.yeij.top/AskaEth/Cyrene/pkg/logger"
"sync"
"time"
)
// Bus 总线接口(方便测试和替换)
type Bus interface {
Publish(event BusEvent)
Subscribe(eventType EventType, handler EventHandler) *Subscription
}
// ConversationBus 对话事件总线
// Step 1: 仅 side-channel 发布,无消费端
type ConversationBus struct {
mu sync.RWMutex
subscribers map[EventType][]*Subscription
eventCh chan BusEvent
done chan struct{}
}
// NewConversationBus 创建总线
func NewConversationBus() *ConversationBus {
b := &ConversationBus{
subscribers: make(map[EventType][]*Subscription),
eventCh: make(chan BusEvent, 64),
done: make(chan struct{}),
}
go b.dispatchLoop()
return b
}
// Publish 发布事件到总线(非阻塞)
func (b *ConversationBus) Publish(event BusEvent) {
if event.Timestamp.IsZero() {
event.Timestamp = time.Now()
}
select {
case b.eventCh <- event:
default:
logger.Printf("[bus] 事件通道已满,丢弃事件: type=%s session=%s", event.Type, event.SessionID)
}
}
// Subscribe 订阅事件类型
func (b *ConversationBus) Subscribe(eventType EventType, handler EventHandler) *Subscription {
b.mu.Lock()
defer b.mu.Unlock()
sub := &Subscription{bus: b, eventType: eventType, handler: handler}
b.subscribers[eventType] = append(b.subscribers[eventType], sub)
return sub
}
// unsubscribe 内部取消订阅
func (b *ConversationBus) unsubscribe(sub *Subscription) {
b.mu.Lock()
defer b.mu.Unlock()
subs := b.subscribers[sub.eventType]
for i, s := range subs {
if s == sub {
b.subscribers[sub.eventType] = append(subs[:i], subs[i+1:]...)
break
}
}
}
// Stop 停止总线
func (b *ConversationBus) Stop() {
close(b.done)
}
// dispatchLoop 后台分发循环
func (b *ConversationBus) dispatchLoop() {
for {
select {
case event := <-b.eventCh:
b.mu.RLock()
subs := b.subscribers[event.Type]
// 拷贝一份避免持锁回调
handlers := make([]EventHandler, len(subs))
for i, s := range subs {
handlers[i] = s.handler
}
b.mu.RUnlock()
for _, h := range handlers {
func() {
defer func() {
if r := recover(); r != nil {
logger.Printf("[bus] handler panic: %v", r)
}
}()
h(event)
}()
}
case <-b.done:
return
}
}
}