当前位置:首页 > 文章列表 > Golang > Go问答 > Google Pub/Sub 消息排序问题导致延迟超过 10 秒?

Google Pub/Sub 消息排序问题导致延迟超过 10 秒?

来源:stackoverflow 2024-03-07 22:45:26 0浏览 收藏

对于一个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知识!

版本声明
本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
Go语言在后端开发中的独特优势探究Go语言在后端开发中的独特优势探究
上一篇
Go语言在后端开发中的独特优势探究
Win10没有移动热点选项的解决方法
下一篇
Win10没有移动热点选项的解决方法
查看更多
最新文章
查看更多
课程推荐
  • 前端进阶之JavaScript设计模式
    前端进阶之JavaScript设计模式
    设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
    542次学习
  • GO语言核心编程课程
    GO语言核心编程课程
    本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
    509次学习
  • 简单聊聊mysql8与网络通信
    简单聊聊mysql8与网络通信
    如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
    497次学习
  • JavaScript正则表达式基础与实战
    JavaScript正则表达式基础与实战
    在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
    487次学习
  • 从零制作响应式网站—Grid布局
    从零制作响应式网站—Grid布局
    本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
    484次学习
查看更多
AI推荐
  • AI边界平台:智能对话、写作、画图,一站式解决方案
    边界AI平台
    探索AI边界平台,领先的智能AI对话、写作与画图生成工具。高效便捷,满足多样化需求。立即体验!
    24次使用
  • 讯飞AI大学堂免费AI认证证书:大模型工程师认证,提升您的职场竞争力
    免费AI认证证书
    科大讯飞AI大学堂推出免费大模型工程师认证,助力您掌握AI技能,提升职场竞争力。体系化学习,实战项目,权威认证,助您成为企业级大模型应用人才。
    49次使用
  • 茅茅虫AIGC检测:精准识别AI生成内容,保障学术诚信
    茅茅虫AIGC检测
    茅茅虫AIGC检测,湖南茅茅虫科技有限公司倾力打造,运用NLP技术精准识别AI生成文本,提供论文、专著等学术文本的AIGC检测服务。支持多种格式,生成可视化报告,保障您的学术诚信和内容质量。
    174次使用
  • 赛林匹克平台:科技赛事聚合,赋能AI、算力、量子计算创新
    赛林匹克平台(Challympics)
    探索赛林匹克平台Challympics,一个聚焦人工智能、算力算法、量子计算等前沿技术的赛事聚合平台。连接产学研用,助力科技创新与产业升级。
    251次使用
  • SEO  笔格AIPPT:AI智能PPT制作,免费生成,高效演示
    笔格AIPPT
    SEO 笔格AIPPT是135编辑器推出的AI智能PPT制作平台,依托DeepSeek大模型,实现智能大纲生成、一键PPT生成、AI文字优化、图像生成等功能。免费试用,提升PPT制作效率,适用于商务演示、教育培训等多种场景。
    194次使用
微信登录更方便
  • 密码登录
  • 注册账号
登录即同意 用户协议隐私政策
返回登录
  • 重置密码