Skip to content

grpc-js: Fix and optimize IDLE timeouts#2643

Merged
murgatroid99 merged 1 commit intogrpc:@grpc/grpc-js@1.9.xfrom
murgatroid99:grpc-js_idle_timer_fix
Jan 16, 2024
Merged

grpc-js: Fix and optimize IDLE timeouts#2643
murgatroid99 merged 1 commit intogrpc:@grpc/grpc-js@1.9.xfrom
murgatroid99:grpc-js_idle_timer_fix

Conversation

@murgatroid99
Copy link
Copy Markdown
Member

This fixes #2642: in the current idle timer implementation, if getConnectivityState(true) is called multiple times, it can lose track of idle timer handles without cancelling them, resulting in those timers firing inappropriately and setting the connectivity state to IDLE when that should not happen.

This change has two layers of defense against that failure: first, the timer now cannot be started if one is already running, so it should not be possible to lose track of existing running timers. Second, the client now tracks the timestamp of the last activity, and the idle timer will only actually set the state to IDLE if enough time has passed since the last activity.

In addition, this second change is part of an optimization to the idle timeout behavior: previously, the timer would start every time the number of active calls increased from 0, and was cancelled every time the number of active calls decreased to 0. This could cause a lot of timer churn if the user was making many requests in sequence, one at a time. Now instead, the timer is started the first time it is reasonable to do so, and after that it runs for the full timeout and then is started again every time the timer fires, with a variable timeout depending on the time since the last activity. As a result, there is only activity related to the idle timer at most twice per timeout period.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants