Google pubsub 流量控制

Google pubsub flow control

我正在尝试实施一项服务,该服务按自己的节奏使用 google pubsub 订阅。那样的话,我的意思是我需要很好地控制何时需要使用消息,即获取一批消息,暂停一段时间,不要接收超过 X 条消息...

使用 google 客户端库我没有找到一种方法来做到这一点,因为 MessageReceiver 运行 在它自己的线程中,我无法控制到底发生了什么。

基本上,能够以同步方式使用消息应该可以解决我的问题。

你知道我如何同步使用 google 客户端库吗?或者我错过的 API 中还有其他方法吗?

您可以尝试使用 setFlowControlSettings when you build your subscriber. In particular, you can use setMaxOutstandingElementCount or setMaxOutstandingRequestBytes 来限制发送到您的 MessageReceiver 的消息。当您有足够多的待处理消息时,即您尚未为其调用 Ack()Nack() 的消息超过这些限制,那么您的 MessageReceiver 将不会被调用,直到消息被 acked 或 nacked。