Prevent trying to open too many connections to the server when the server is closing the websocket on its own #74
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See #67
Currently when the server decides to close the websocket after an authentication failure y-websocket immediately retries to connect since the wsUnsuccessfulReconnects is reinitialized as soon as the onopen event occurs.
This pr adds a 1 second delay before resetting the wsUnsuccessfulReconnects which will allow for the reconnection timeout to be greater than 0 and avoid trying to reopen the connection on every tick which can easily overload the server with a few clients.
If the connection is closed within 1 second of opening, the resetting of wsUnsuccessfulReconnects is canceled and the number of wsUnsuccessfulReconnects will keep growing.
setTimeout solution, really?
I'm usually very much against using timers to "fix" a behavior, it's almost always wrong but here the goal is to determine when a connection is successful. A connection that fails to authenticate is obviously not a successful connection and since we can't authenticate using headers with websockets (Except by hacking the protocol header, which I think stinks a little) we need to establish the websocket connection before closing it because of an authentication failure.
The other main reason why I think setTimeout is a proper solution here is that it specifically only affects the reconnection interval logic. It was obviously not intended to try reconnecting hundreds of times a second and this fix still allows for the exact same logic to happen, and to consider a 1 second connection a "successful connection" to reset the reconnection interval algorithm.
Huly®: YJS-727