pubsub: stop creating negative timeouts - #9243
Conversation
There was a problem hiding this comment.
Code Review
This pull request updates the lease manager to prevent negative timeouts by ensuring the calculated deadline extension is at least 0. The reviewer suggests enforcing a minimum delay of 1000ms instead of 0 to prevent a potential storm of rapid, back-to-back lease extension requests when latency is high.
| const latency = this._subscriber.modAckLatency; | ||
|
|
||
| return (deadline * 0.9 - latency) * jitter; | ||
| return Math.max(0, deadline * 0.9 - latency) * jitter; |
There was a problem hiding this comment.
While using Math.max(0, ...) prevents negative timeouts and the resulting Node.js warnings, returning 0 when latency exceeds 90% of the deadline can lead to immediate rescheduling of lease extensions. Under high latency or network congestion, this can cause a storm of rapid, back-to-back modAck requests, further exacerbating the latency issue.
Consider enforcing a reasonable minimum delay (e.g., 1000ms) to prevent spamming the server when latency is high.
| return Math.max(0, deadline * 0.9 - latency) * jitter; | |
| return Math.max(1000, deadline * 0.9 - latency) * jitter; |
I didn't create an issue because it had so many required steps for a one-line fix, and those steps weren't applicable in this case.
This code assumes that latency is less than 90% of the deadline, but in our production environment we see that's not always the case. When it isn't, this library tries to create a timeout with negative duration, which throws a node
TimeoutNegativeWarning.Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:
Fixes #<issue_number_goes_here> 🦕