Close 关闭事件总线,释放资源
()
| 154 | |
| 155 | // Close 关闭事件总线,释放资源 |
| 156 | func (eb *EventBus) Close() { |
| 157 | // 先停止清理 worker(不持锁,避免死锁) |
| 158 | if eb.cleanupDone != nil { |
| 159 | close(eb.cleanupDone) |
| 160 | eb.cleanupWg.Wait() // 等待清理 goroutine 完全退出 |
| 161 | eb.cleanupDone = nil |
| 162 | } |
| 163 | |
| 164 | // 然后清理所有资源(持锁) |
| 165 | eb.mu.Lock() |
| 166 | defer eb.mu.Unlock() |
| 167 | |
| 168 | // 关闭所有订阅 channel |
| 169 | for id, ch := range eb.progressSubs { |
| 170 | close(ch) |
| 171 | delete(eb.progressSubs, id) |
| 172 | } |
| 173 | for id, ch := range eb.controlSubs { |
| 174 | close(ch) |
| 175 | delete(eb.controlSubs, id) |
| 176 | } |
| 177 | for id, ch := range eb.monitorSubs { |
| 178 | close(ch) |
| 179 | delete(eb.monitorSubs, id) |
| 180 | } |
| 181 | |
| 182 | // 清空数据 |
| 183 | eb.timeline = nil |
| 184 | eb.bookmarks = nil |
| 185 | } |
| 186 | |
| 187 | // emit 发送事件到总线(内部方法) |
| 188 | func (eb *EventBus) emit(channel types.AgentChannel, event any) types.AgentEventEnvelope { |