返回资讯中心
外部精选
软件工程
#开发者实时流

开发者详解生产环境中SSE流架构的端到端实现

来源标题:The Architecture Behind Our SSE Streams

开发者Robert Ross发文详解其平台生产环境中Server-Sent Events(SSE)流的完整架构,指出难点不在向HTTP客户端推流,而在消息如何进入该流。他批评多数技术博客只提

Hacker News 最新andsoitis2026/11/8 · 9 天前30 分钟阅读

以下正文由 AI 翻译自 Hacker News 最新(原文版权归原站所有),可能存在偏差,重要内容请以原文为准。

我们是一群热爱动手构建的开发者。我们的目标是打造一个 yes-code 平台,让构建 agent 的过程拥有软件开发中最令人享受的那部分体验。

我们的 SSE 流背后的架构

Nerding Out 2026 年 9 月 17 日

$ whoami

Robert Ross

$ date

Thu Sep 17 8:15:47 EDT 2026

$ pwd

Blue Bottle, 40 Bow St, Cambridge, MA 02138

如今我目光所及之处,每个产品里都少不了一个聊天框。如果 AIM 活到今天,看到自己对现代 Web 的影响一定会非常欣慰:人类就是爱聊天。文本框、提交按钮,就这么简单。

Server-Side Events(SSE)的难点,并不在于把一条消息流式推送给 HTTP 客户端。这件事现在已经很简单了,每种语言都有大量开源包可用。

真正难的是:怎么让一条消息先进入那条流。

我发现,大多数讨论 SSE 的技术博客视角都很狭隘。它们跳过了数据如何进入系统、以及如何在系统之外流转这些环节。它们只会说一句“用 Redis 吧”,然后就没了。

这篇文章不是那样的。它是端到端的技术细节,是亲手写的,而且已经在生产环境中运行。

先确认一下这里的 SSE 指的是什么:本文讲的并不是像你在 LLM API 里看到的那种文本 chunk 流式输出;不过背后的技术思路是一样的。

Cadenya 中大量使用 CQRS 设计模式,这意味着数据存储拥有一致的接口设计。规则很简单:

各个 repository 以这样的包结构存放:

internal/repositories/tools/repository.go internal/repositories/objectives/repository.go internal/repositories/agentschedules/repository.go

每个包都会导出一个 Repository 接口:

// Repository defines the interface for objective operations. type Repository interface { CreateObjective(ctx context.Context, req *apiv1.CreateObjectiveRequest, opts ...CreateObjectiveOption) (*apiv1.Objective, error) GetObjective(ctx context.Context, req *apiv1.GetObjectiveRequest) (*apiv1.Objective, error) GetObjectiveDiagnostics(ctx context.Context, req *apiv1.GetObjectiveDiagnosticsRequest) (*apiv1.GetObjectiveDiagnosticsResponse, error) ListObjectives(ctx context.Context, req *apiv1.ListObjectivesRequest) (*apiv1.ListObjectivesResponse, error) ContinueObjective(ctx context.Context, req *apiv1.ContinueObjectiveRequest) (*apiv1.ObjectiveEvent, error) CreateContextWindow(ctx context.Context, objectiveID string) (*apiv1.ObjectiveContextWindow, error)

repository 类型的方法参数和返回类型始终使用 protobuf。这些领域的接口通常也与 gRPC RPC 方法保持一致,因此服务端实现基本上就是委托给 repository。例如,这是 agent schedules 的一个 RPC 方法:

func (os *ObjectiveService) objectiveRepo(ctx context.Context) objectives.Repository { return objectives.NewRepository(os.GetDB(), &os.PubSub, os.masterKey, os.eventStore(ctx)) func (os *ObjectiveService) CreateObjective(ctx context.Context, req *apiv1.CreateObjectiveRequest) (*apiv1.Objective, error) { return os.objectiveRepo(ctx).CreateObjective(ctx, req)

这种薄设计意味着,无论数据写入数据库还是从数据库读取,使用的始终是同一个接口。

在 Cadenya 中创建 Objective 时,它会经过 objectives.Repository 接口,进入我们的 Postgres 数据库,并进入 Google Pub/Sub。记录一旦存入 Postgres,就会被物化为对应的 Protobuf 消息。(这就是本文中到处出现的 apiv1 包。)

我们的事件发布机制里有一点“小把戏”,用(也可以说是滥用?)Go 的泛型来实现一个发布消息的函数。内部有一些 topicsv1 protobuf 消息,充当信封,用来表示 Pub/Sub 中的事件。例如:

syntax = "proto3"; package topics.v1; import "cadenya/api/v1/objective_context.proto"; import "cadenya/api/v1/objectives.proto"; option go_package = "go.cadenya.dev/internal/proto/topics/v1;topicsv1"; message ObjectiveCreated { cadenya.api.v1.Objective objective = 1; cadenya.api.v1.ObjectiveContextWindow context_window = 2;

当这条消息由 buf 生成后,就可以传入我们 pubsub 包里的 Publisher 函数,借助 protoreflect 生成 topic 名称。例如:

// PublishFunc publishes only T using the context bound by NewPublisher. // Bind publishers within a request or activity so cancellation and tracing follow it. type PublishFunc[T proto.Message] func(T, ...PublishOption) error // NewPublisher binds a protobuf type to its topic and a publishing context. // Go version 1.27 allows using generics on methods, so we can use T directly. func (ps *PubSub) NewPublisher[T proto.Message](ctx context.Context) (PublishFunc[T], error) { return NewPublisher[T](ctx, ps) // NewPublisher binds an interface-backed publisher to a protobuf type and context. // Concrete PubSub dependencies also expose this as a generic method. func NewPublisher[T proto.Message](ctx context.Context, backend Publisher) (PublishFunc[T], error) { var zero T message := zero.ProtoReflect().New().Interface() publish, err := backend.Publisher(message) if err != nil { return nil, err return func(msg T, opts ...PublishOption) error { if !msg.ProtoReflect().IsValid() { return fmt.Errorf("pubsub: cannot publish nil %s", TopicName(message)) return publish(ctx, applyPublishOptions(opts).key, msg, opts...) }, nil

backend.Publisher 是一个可注入的接口,测试时可以替换为 Noop 实现。如果哪天我胆大起来,这部分也可以换成 Kafka。而 publisher 函数返回的函数,会根据 proto 消息本身把消息发送到对应的 topic:

// Publisher returns a RawPublishFunc for the given message type. func (ps *PubSub) Publisher(msg proto.Message) (RawPublishFunc, error) { // Converts the message to topicsv1.ObjectiveCreated for example topicName := string(msg.ProtoReflect().Descriptor().FullName())

(原文较长,此处译出前 6000 字,完整内容请点「打开原文」。)

正文由 探How 从来源站点 RSS 同步,内容未经改写;遇到排版缺失或需要图片、视频时请以原文为准。

打开原文
去社区讨论 →