Go GCP Cloud PubSub 不批量发布消息

Go GCP Cloud PubSub not batch publishing messages

我正在开发一个示例项目,该项目从 bigquery 获取输出并将其发布到 pubsub。 bigquery 的行输出可能大于 100,000。我看到有批量发布的选项,而且我在多个地方读到每批 1k 条消息是理想的。我 运行 遇到的问题是,在我的一生中,我无法让它批量处理多条消息,我认为解决方案很简单,但我不知道如何去做..

这是我现在拥有的,它所做的只是一次发布一条消息。

func publish(client pubsub.Client, data []byte) (string, error) {
    ctx := context.Background()

    topic := client.Topic("topic-name")
    topic.PublishSettings = pubsub.PublishSettings{
        // ByteThreshold:  5000,
        CountThreshold: 1000, // no matter what I put here it still sends one per publish
        // DelayThreshold: 1000 * time.Millisecond,
    }

    result := topic.Publish(ctx, &pubsub.Message{
        Data: data,
    })

    id, err := result.Get(ctx)
    if err != nil {
        return "", err
    }

    return id, nil
}

并且此函数被调用:

for _, v := range qr {
        data, err := json.Marshal(v)
        if err != nil {
            log.Printf("Unable to marshal %s", data)
            continue
        }
        id, err := publish(*pubsubClient, data)
        if err != nil {
            log.Printf("Unable to publish message: %s", data)
        }
        log.Printf("Published message with id: %s", id)
    }

其中 qr 是包含从 bigquery 查询返回的数据的结构片段。

现在,是不是因为我调用函数 publish 的方式导致每条消息都被发布,并且 topic.PublishSettings 在每次方法调用时都被覆盖,所以它忘记了之前的消息?我在这里不知所措。

我在这里看到了部分批量发布代码:https://github.com/GoogleCloudPlatform/golang-samples/blob/master/pubsub/topics/main.go#L217

但他们实际上并没有在他们的样本中调用它,所以我不知道应该怎么做。

旁注并进一步证明我的观点它不起作用,如果我在 topic.PublishSettings var 中设置 DelayThreshold 说,1 秒,它只是每秒发布一条消息,不是所有应该在内存中的消息。

感谢您的帮助,谢谢。

编辑#1:

所以根据 kingkupps 的评论,我将代码切换为以下代码以用于测试目的:(项目和主题名称从真实名称切换)

func QueryAndPublish(w http.ResponseWriter, r *http.Request) {
    ctx := context.Background()
    // setting up the pubsub client
    pubsubClient, err := pubsub.NewClient(ctx, "fake-project-id")
    if err != nil {
        log.Fatalf("Unable to get pubsub client: %v", err)
    }

    // init topic and settings for publishing 1000 messages in batch
    topic := pubsubClient.Topic("fake-topic")
    topic.PublishSettings = pubsub.PublishSettings{
        // ByteThreshold:  5000,
        CountThreshold: 1000,
        // DelayThreshold: 1000 * time.Millisecond,
    }

    // bq set up
    bqClient, err := bigquery.NewClient(ctx, "fake-project-id")
    if err != nil {
        log.Fatalf("Unable to get bq client: %v", err)
    }
    // bq query function call
    qr, err := query(*bqClient)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("Got query results, publishing now")

    // marshalling messages to json format
    messages := make([][]byte, len(qr))
    timeToMarshal := time.Now()
    for i, v := range qr {
        data, err := json.Marshal(v)
        if err != nil {
            log.Printf("Unable to marshal %s", data)
            continue
        }
        messages[i] = data
    }
    elapsedMarshal := time.Since(timeToMarshal).Nanoseconds() / 1000000
    log.Printf("Took %v ms to marshal %v messages", elapsedMarshal, len(messages))

    // publishing messages
    timeToPublish := time.Now()
    publishCount := 0
    for _, v := range messages {
        // ignore result, err from topic.Publish return, just publish
        topic.Publish(ctx, &pubsub.Message{
            Data: v,
        })
        publishCount++
    }
    elapsedPublish := time.Since(timeToPublish).Nanoseconds() / 1000000
    log.Printf("Took %v ms to publish %v messages", elapsedPublish, publishCount)

    fmt.Fprint(w, "Job completed")
}

现在,当我的消息计数为 100,000 时,它会在大约 600 毫秒内完成发布调用,但在后台,它仍会一个接一个地发布到 pubsub 端点。

我可以在 StackDriver 和 Wireshark 中看到这一点,其中我在 Stackdriver 中的 messages/second 大约是 10-16/秒并且 Wireshark 显示每条消息发送的新连接。

这可能是因为当您调用

topic.PublishSettings = pubsub.PublishSettings{ // ByteThreshold: 5000, CountThreshold: 1000, // DelayThreshold: 1000 * time.Millisecond, }

您正在将发布设置重置为零初始化结构。这会将 topic.PublishSettings.ByteThreshold 设置为 0,这意味着所有消息将立即发布;你告诉它等到它有 0 个字节,它总是有 0 个字节。

相反,您应该执行以下操作来设置 CountThreshold:

topic.PublishSettings.CountThreshold = 1000

其他领域也是如此。它们已经初始化为默认值,如 here 所述,如果您想更改它们,请直接修改它们,而不是重新分配整个 PublishSetttings 对象。