从GCP发布/订阅中捕获错误代码 [英] Catch error code from GCP pub/sub

查看:82
本文介绍了从GCP发布/订阅中捕获错误代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用go包进行发布/订阅.在我的API仪表板上,我看到此错误(google.pubsub.v1.Subscriber.StreamingPull-错误代码503).每个文档( https://cloud.google.com/pubsub/docs/reference /error-codes ),这似乎是暂时情况,但最好实施退避策略(

I am using go package for pub/sub. On my API dashboard I see this error(google.pubsub.v1.Subscriber.StreamingPull - error code 503). Per docs(https://cloud.google.com/pubsub/docs/reference/error-codes) it seems it is transient condition but better to implement backoff strategy(https://cloud.google.com/storage/docs/exponential-backoff). the question is I am not able to wrap my head where this error code is coming on Receive method.

这里很有趣:

err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
        // Dump message
        // log.Printf("Got message: %s", m.Data)

        // Decoding coming message
        err = json.NewDecoder(bytes.NewReader(m.Data)).Decode(&msg)
        if err != nil {
            log.Printf("Error decoding - %v", err)
        }

        // See streaming messages
        log.Printf(" %s : %s : Product updated for Product Id(%d) : Product Title(%s)",
            msg.AuthID,
            msg.TraceID,
            msg.Product.ID,
            msg.Product.Title,
        )

        //
        // Some business logic
        //


        // Acknowledge on recieve method
        m.Ack()
    })

    if err != context.Canceled {
        // if err != nil {
        return errors.Wrap(err, "Error occurred on recieve data from topic: blah")
    }

推荐答案

Cloud Pub/Sub Go客户端库会自行重试此暂时性错误,您无需进行处理.在内部,客户端库使用 StreamingPull ,它在其中发送请求并从服务接收消息(如果有).有时,可能发生断开连接事件,要求重新建立连接.这就是为什么您在API仪表板中看到503错误的原因.在这种情况下,您的代码不应看到错误,因为底层库正在处理它(包括使用指数补偿)(在相关情况下).

The Cloud Pub/Sub Go client library will retry this transient error on its own, you shouldn't need to handle it. Internally, the client library uses StreamingPull, where it sends a request and receives messages from the service as they are available. Occasionally, there can be a disconnection event requiring the connection to be reestablished. This is why you see the 503 error in the API dashboard. It should not be the case that your code sees an error in this scenario since the underlying library is handling it (including the use of exponential backoff, where relevant).

这篇关于从GCP发布/订阅中捕获错误代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆