[错误] 使用 redisnats 代理时,带有标签的广播会导致服务器崩溃
作者: darakanoit创建于 2026年9月29日更新于 2026年9月29日
描述错误。NatsBroker.PublishWithStreamPosition 会直接将内部 epoch 标签写入 opts.Tags 中,这是一个属于调用者的映射:
tags := opts.Tags
if tags == nil {
tags = map[string]string{}
}
tags[epochTagsKey] = sp.EpochExecutor.Broadcast 会在自己的 goroutine 中向每个通道发布消息,并将相同的 cmd.Tags 映射传递给它们(centrifuge.WithTags 不会复制它)。使用 redisnats 代理的情况下,每个使用历史或 idempotency 标签的发布都会通过 PublishWithStreamPosition 进行。因此,多个 goroutine 会同时写入同一个映射,而其他 goroutine 则在 RedisBroker.Publish 中读取或序列化它。进程会以无法恢复的致命错误结束:并发映射写入、并发映射迭代和映射写入 或 并发映射读取和映射写入。
内容来源: centrifugal/centrifugo