简体   繁体   English

Google Pub/Sub 如何在 Pull 上设置读取超时

[英]Google Pub/Sub How to set read timeout on Pull

I would like to set the read timeout of the pull request on a subscription.我想在订阅上设置拉取请求的读取超时。 Right now the only options are to set returnImmediately=true or just wait until the pubsub returns, which seems to be 90 seconds if no messages is published.现在唯一的选择是设置returnImmediately=true或者只是等到 pubsub 返回,如果没有消息发布,这似乎是 90 秒。

I'm using the gcloud-node module to make calls to pubsub.我正在使用 gcloud-node 模块调用 pubsub。 It uses the request module under the hood to make the the gcloud api calls.它使用引擎盖下的请求模块来进行 gcloud api 调用。 I've updated my local copy of gcloud-node/lib/pubsub/subscription.js to set the request timeout to 30 seconds我已经更新了gcloud-node/lib/pubsub/subscription.js 的本地副本,将请求超时设置为 30 秒

this.request({
  method: 'POST',
  uri: ':pull',
  timeout: 30000,
  json: {
    returnImmediately: !!options.returnImmediately,
    maxMessages: options.maxResults
  }
}

When I do this, the behavior I see is the connection will timeout on the client side after 30 seconds, but pubsub still has the request open.当我这样做时,我看到的行为是连接将在 30 秒后在客户端超时,但 pubsub 仍然打开请求。 If I have two clients pulling on the subscription and one of them timeout after 30 seconds, then a message is published to the topic, it is a 50/50 chance that the remaining listening client will retrieve the message.如果我有两个客户端拉取订阅,并且其中一个客户端在 30 秒后超时,那么将向该主题发布一条消息,剩余的侦听客户端将有 50/50 的机会检索该消息。

Is there a way to tell pubsub to timeout pull connections after a certain amount of time?有没有办法告诉pubsub在一定时间后超时拉连接?

UPDATE: I probably need to clarify my example a bit.更新:我可能需要稍微澄清一下我的例子。 I have two clients that connect at the same time and pull from the same subscription.我有两个客户端同时连接并从同一个订阅中提取。 The only difference between the two is that the first one is configured to timeout after 30 seconds.两者的唯一区别是第一个配置为在 30 秒后超时。 Since two clients are connected to the same subscription, pubsub will distribute the message load between the two of them.由于两个客户端连接到同一个订阅,pubsub 将在它们两个之间分配消息负载。 If I publish a message 45 seconds after both clients connect, there is a 50/50 chance that pubsub will deliver the message to the second client that has not timed out yet.如果我在两个客户端连接后 45 秒发布消息,则 pubsub 有 50/50 的机会将消息传递给尚未超时的第二个客户端。 If I send 10 messages instead of just one, the second client will receive a subset of the 10 messages.如果我发送 10 条消息而不是一条消息,第二个客户端将收到 10 条消息的子集。 It looks like this is because my clients are in a long poll.看起来这是因为我的客户正在进行长时间的投票。 If the client disconnects, the server has no idea and will try to send published messages on the response of the request that was made by the client that has timed out.如果客户端断开连接,服务器不知道并且将尝试根据已超时的客户端发出的请求的响应发送已发布的消息。 From my tests, this is the behavior I've observed.根据我的测试,这是我观察到的行为。 What I would like to do is be able to send a timeout param in the pull request to tell subpub to send back a response after a 30000ms if no messages are published during that time.我想要做的是能够在拉取请求中发送一个超时参数,以告诉 subpub 在 30000 毫秒后发回响应,如果在那段时间内没有发布消息。 Reading over the API docs , this doesn't seem like an option.阅读API 文档,这似乎不是一个选项。

Setting the request timeout as you have is the correct way to timeout the pull after 30 seconds.设置请求超时是在 30 秒后超时拉取的正确方法。 The existence of the canceled request might not be what is causing the other pull to not get the message immediately.被取消的请求的存在可能不是导致另一个 pull 不能立即收到消息的原因。 If your second pull (that does not time out) manages to pull other messages that were published earlier, it won't necessarily wait for additional message that was published after the timeout to come in before completing.如果您的第二次拉取(未超时)设法拉取之前发布的其他消息,则它不一定会等待超时后发布的其他消息在完成之前进入。 It only guarantees to not return more than maxMessages , not to return only once it has exactly maxMessages (if that many are available).它既能保证不返回超过maxMessages ,不返回只有一次有确切maxMessages (如果很多可用)。 Once your publish completes, some later pull will get the message, but there are no guarantees on exactly when that will occur.一旦您的发布完成,稍后的一些拉取将收到该消息,但无法保证确切的发生时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM