Google Pub/Sub 消息排序问题导致延迟超过 10 秒?
对于一个Golang开发者来说,牢固扎实的基础是十分重要的,golang学习网就来带大家一点点的掌握基础知识点。今天本篇文章带大家了解《Google Pub/Sub 消息排序问题导致延迟超过 10 秒?》,主要介绍了,希望对大家的知识积累有所帮助,快点收藏起来吧,否则需要时就找不到了!
我正在尝试制作一个简化的示例,演示如何使用 google pub/sub 的消息排序功能 (https://cloud.google.com/pubsub/docs/ordering)。从这些文档中,在为订阅启用消息排序后,
设置消息排序属性后,pub/sub 服务将按照 pub/sub 服务接收消息的顺序传递具有相同排序键的消息。例如,如果发布者发送具有相同排序键的两条消息,则 pub/sub 服务将首先传送最旧的消息。
我用它来编写以下示例:
package main
import (
"context"
"log"
"time"
"cloud.google.com/go/pubsub"
uuid "github.com/satori/go.uuid"
)
func main() {
client, err := pubsub.newclient(context.background(), "my-project")
if err != nil {
log.fatalf("newclient: %v", err)
}
topicid := "test-topic-" + uuid.newv4().string()
topic, err := client.createtopic(context.background(), topicid)
if err != nil {
log.fatalf("createtopic: %v", err)
}
defer topic.delete(context.background())
subid := "test-subscription-" + uuid.newv4().string()
sub, err := client.createsubscription(context.background(), subid, pubsub.subscriptionconfig{
topic: topic,
enablemessageordering: true,
})
if err != nil {
log.fatalf("createsubscription: %v", err)
}
defer sub.delete(context.background())
ctx, cancel := context.withcancel(context.background())
defer cancel()
messagereceived := make(chan struct{})
go sub.receive(ctx, func(ctx context.context, msg *pubsub.message) {
log.printf("received message with ordering key %s: %s", msg.orderingkey, msg.data)
msg.ack()
messagereceived <- struct{}{}
})
topic.publish(context.background(), &pubsub.message{data: []byte("dang1!"), orderingkey: "foobar"})
topic.publish(context.background(), &pubsub.message{data: []byte("dang2!"), orderingkey: "foobar"})
for i := 0; i < 2; i++ {
select {
case <-messagereceived:
case <-time.after(10 * time.second):
log.fatal("expected to receive a message, but timed out after 10 seconds.")
}
}
}
首先,我尝试了该程序,但没有在 topic.publish() 调用中指定 orderingkey: "foobar" 。这导致了以下输出:
> go run main.go 2020/08/10 21:40:34 received message with ordering key : dang2! 2020/08/10 21:40:34 received message with ordering key : dang1!
换句话说,消息接收的顺序与发布的顺序不同,这在我的用例中是不可取的,我想通过指定 orderingkey 来防止
但是,当我在发布调用中添加 orderingkeys 时,程序在等待接收 pub/sub 消息 10 秒后超时:
> go run main.go 2020/08/10 21:44:36 Expected to receive a message, but timed out after 10 seconds. exit status 1
我期望现在首先收到消息 dang1! 然后是 dang2!,但我没有收到任何消息。知道为什么这没有发生吗?
解决方案
发布失败并出现以下错误:无法发布:topic.enablemessageordering=false,但在消息中设置了 orderingkey。请删除 orderingkey 或打开 topic.enablemessageordering。
如果您更改发布调用以检查错误,您可以看到这一点:
res1 := topic.publish(context.background(), &pubsub.message{data: []byte("dang1!"), orderingkey: "foobar"})
res2 := topic.publish(context.background(), &pubsub.message{data: []byte("dang2!"), orderingkey: "foobar"})
_, err = res1.get(ctx)
if err != nil {
fmt.printf("failed to publish: %v", err)
return
}
_, err = res2.get(ctx)
if err != nil {
fmt.printf("failed to publish: %v", err)
return
}
要修复此问题,请添加一行以启用主题的消息排序。您的主题创建如下:
topic, err := client.createtopic(context.background(), topicid)
if err != nil {
log.fatalf("createtopic: %v", err)
}
topic.enablemessageordering = true
defer topic.delete(context.background())我独立提出了与 kamal 相同的解决方案,只是想分享完整修改后的实现:
package main
import (
"context"
"flag"
"log"
"time"
"cloud.google.com/go/pubsub"
uuid "github.com/satori/go.uuid"
)
var enablemessageordering bool
func main() {
flag.boolvar(&enablemessageordering, "enablemessageordering", false, "enable and use pub/sub message ordering")
flag.parse()
client, err := pubsub.newclient(context.background(), "fleetsmith-dev")
if err != nil {
log.fatalf("newclient: %v", err)
}
topicid := "test-topic-" + uuid.newv4().string()
topic, err := client.createtopic(context.background(), topicid)
if err != nil {
log.fatalf("createtopic: %v", err)
}
topic.enablemessageordering = enablemessageordering
defer topic.delete(context.background())
subid := "test-subscription-" + uuid.newv4().string()
sub, err := client.createsubscription(context.background(), subid, pubsub.subscriptionconfig{
topic: topic,
enablemessageordering: enablemessageordering,
})
if err != nil {
log.fatalf("createsubscription: %v", err)
}
defer sub.delete(context.background())
ctx, cancel := context.withcancel(context.background())
defer cancel()
messagereceived := make(chan struct{})
go sub.receive(ctx, func(ctx context.context, msg *pubsub.message) {
log.printf("received message with ordering key %s: %s", msg.orderingkey, msg.data)
msg.ack()
messagereceived <- struct{}{}
})
msg1, msg2 := &pubsub.message{data: []byte("dang1!")}, &pubsub.message{data: []byte("dang2!")}
if enablemessageordering {
msg1.orderingkey, msg2.orderingkey = "foobar", "foobar"
}
publishmessage(topic, msg1)
publishmessage(topic, msg2)
for i := 0; i < 2; i++ {
select {
case <-messagereceived:
case <-time.after(10 * time.second):
log.fatal("expected to receive a message, but timed out after 10 seconds.")
}
}
}
func publishmessage(topic *pubsub.topic, msg *pubsub.message) {
publishresult := topic.publish(context.background(), msg)
messageid, err := publishresult.get(context.background())
if err != nil {
log.fatalf("get: %v", err)
}
log.printf("published message with id %s", messageid)
}
当调用 enablemessageordering 标志设置为 true 时,我首先收到 dang1!,然后收到 dang2!:
> go run main.go --enablemessageordering 2020/08/11 05:38:07 published message with id 1420685949616723 2020/08/11 05:38:08 published message with id 1420726763302425 2020/08/11 05:38:09 received message with ordering key foobar: dang1! 2020/08/11 05:38:11 received message with ordering key foobar: dang2!
如果没有它,我会以与以前相反的顺序收到它们:
> go run main.go 2020/08/11 05:38:47 Published message with ID 1420687395091051 2020/08/11 05:38:47 Published message with ID 1420693737065665 2020/08/11 05:38:48 Received message with ordering key : Dang2! 2020/08/11 05:38:48 Received message with ordering key : Dang1!
好了,本文到此结束,带大家了解了《Google Pub/Sub 消息排序问题导致延迟超过 10 秒?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
Go语言在后端开发中的独特优势探究
- 上一篇
- Go语言在后端开发中的独特优势探究
- 下一篇
- Win10没有移动热点选项的解决方法
-
- Golang · Go问答 | 41分钟前 | 错误处理 · go · 文件系统 · errors.Is io/fs fs.ErrNotExist fs.ErrPermission
- Go io/fs文件不存在与权限错误的分类处理
- 259浏览 收藏
-
- Golang · Go问答 | 23小时前 |
- Go time.NewTimer替代高频time.After的资源控制方法
- 481浏览 收藏
-
- Golang · Go问答 | 1天前 | go · archive/zip FileHeader.Name Go zip ZIP目录条目 Go压缩包遍历
- Go zip压缩包目录条目为空时的遍历兼容方案
- 181浏览 收藏
-
- Golang · Go问答 | 1天前 |
- Go tar归档中文文件名读取乱码时的编码边界
- 244浏览 收藏
-
- Golang · Go问答 | 1天前 | 配置管理 · go · 配置文件 相对路径 Go path/filepath 工作目录
- Go path/filepath相对路径在不同工作目录下失效的定位顺序
- 401浏览 收藏
-
- Golang · Go问答 | 1天前 | Go问答 · Go nil JSONHandler log/slog TextHandler
- Go log/slog属性值为nil时的输出差异排查
- 276浏览 收藏
-
- Golang · Go问答 | 2天前 |
- Go t.Cleanup注册后测试提前Fatal的资源释放边界
- 399浏览 收藏
-
- Golang · Go问答 | 3天前 |
- Go testing t.Parallel与t.Setenv冲突时的组织方式
- 265浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 226次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 273次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 235次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 219次使用
-
- CMMLU
- 深入了解CMMLU中文评估基准,涵盖67个学科主题,提供数据集下载、Zero-shot/Five-shot评估方法及排行榜,助力优化中文语言模型性能。
- 210次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go sql.Tx提交成功前读取结果导致事务边界混乱的修复方法
- 2026-09-20 501浏览
-
- Go select 用 time.After 做超时有什么资源代价
- 2026-09-10 501浏览
-
- Go 取 range 变量地址为什么得到重复指针
- 2026-09-07 501浏览

