从 GCP 捕获错误代码 pub/sub
Catch error code from GCP pub/sub
我正在为 pub/sub 使用 go 包。在我的 API 仪表板上,我看到了这个错误(google.pubsub.v1.Subscriber.StreamingPull - 错误代码 503)。根据文档(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)。问题是我无法在 Receive 方法出现此错误代码的地方全神贯注。
这是函数:
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 client library will retry this transient error on its own, you shouldn't need to handle it. Internally, the client library uses StreamingPull,它发送请求并在服务可用时从服务接收消息。有时,可能会出现需要重新建立连接的断开连接事件。这就是您在 API 仪表板中看到 503 错误的原因。在这种情况下,您的代码不应该看到错误,因为底层库正在处理它(包括使用相关的指数退避)。
我正在为 pub/sub 使用 go 包。在我的 API 仪表板上,我看到了这个错误(google.pubsub.v1.Subscriber.StreamingPull - 错误代码 503)。根据文档(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)。问题是我无法在 Receive 方法出现此错误代码的地方全神贯注。
这是函数:
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 client library will retry this transient error on its own, you shouldn't need to handle it. Internally, the client library uses StreamingPull,它发送请求并在服务可用时从服务接收消息。有时,可能会出现需要重新建立连接的断开连接事件。这就是您在 API 仪表板中看到 503 错误的原因。在这种情况下,您的代码不应该看到错误,因为底层库正在处理它(包括使用相关的指数退避)。