vault backup: 2026-06-03 10:30:42

This commit is contained in:
2026-06-03 10:30:42 +08:00
parent 2e207dc8fb
commit b5d9e44f0b
16 changed files with 661 additions and 507 deletions
+26 -18
View File
@@ -1,16 +1,25 @@
# 08 — SSE 实时推送
---
tags: [sse, event-stream, pub-sub, real-time, websockets-alternative, go]
create time: 2026-06-03 10:35
---
> **一句话概括**:内存 EventBus 发布/订阅,SSE 推送管线进度到浏览器,让用户实时看到生成过程。
# 08. SSE 实时推送
## 概述
内存 EventBus 发布/订阅,SSE 推送管线进度到浏览器,让用户实时看到生成过程。
---
## 正文
```mermaid
flowchart LR
P["⚙️ Pipeline<br/>Callback"] -->|"Publish"| EB["📡 EventBus<br/>Broker"]
EB -->|"Subscribe<br/>taskID"| H1["🌐 SSE Handler<br/>/tasks/:id/stream"]
EB -->|"SubscribeAll<br/>global"| H2["🌐 SSE Handler<br/>/projects/:id/stream"]
H1 -->|"text/event-stream"| B1["🖥️ Browser<br/>EventSource"]
H2 -->|"text/event-stream"| B2["🖥️ Browser<br/>EventSource"]
P["Pipeline Callback"] -->|"Publish"| EB["EventBus Broker"]
EB -->|"Subscribe taskID"| H1["SSE Handler /tasks/:id/stream"]
EB -->|"SubscribeAll global"| H2["SSE Handler /projects/:id/stream"]
H1 -->|"text/event-stream"| B1["Browser EventSource"]
H2 -->|"text/event-stream"| B2["Browser EventSource"]
style P fill:#e8f5e9,stroke:#388e3c
style EB fill:#fff3e0,stroke:#f57c00
@@ -22,7 +31,7 @@ flowchart LR
---
## 📡 EventBus 架构
## EventBus 架构
EventBus 是 Gen2D 的内存事件总线,负责在 Pipeline 执行过程中发布进度事件,并由 SSE Handler 订阅推送给客户端。
@@ -57,7 +66,7 @@ type TaskEvent struct {
---
## 🔔 订阅模式
## 订阅模式
### Subscribe(taskID) — 任务级订阅
@@ -93,7 +102,7 @@ func (b *Broker) SubscribeAll() <-chan TaskEvent {
---
## 📤 Publish — 扇出分发
## Publish — 扇出分发
```go
func (b *Broker) Publish(taskID string, event TaskEvent) {
@@ -119,7 +128,7 @@ func (b *Broker) Publish(taskID string, event TaskEvent) {
---
## 🌐 SSE Handler
## SSE Handler
### Stream — 任务级流
@@ -175,7 +184,7 @@ GET /api/v1/projects/:projectId/stream
---
## 📊 数据流全景
## 数据流全景
```mermaid
sequenceDiagram
@@ -199,7 +208,7 @@ sequenceDiagram
---
## 🛡️ 容错设计
## 容错设计
| 场景 | 处理方式 |
|------|---------|
@@ -211,9 +220,8 @@ sequenceDiagram
---
## 🔗 关联文档
## 关联文档
- [← 返回索引](00-index.md)
- [05 — 生成管线](05-generation-pipeline.md) — Pipeline 中的进度回调
- [07 — 可观测性](07-observability.md) — SSE 连接的监控
- [10 — 中间件链](10-middleware-chain.md) — SSE 端点的中间件配置
- [[05-生成管线]] — Pipeline 中的进度回调
- [[07-可观测性]] — SSE 连接的监控
- [[10-中间件链]] — SSE 端点的中间件配置