Skip to content

Commit 5e923f3

Browse files
committed
Improve cloud variable backoff logic
Our cloud variable server refuses connections when there are too many people in the same room. The client does have backoff logic, however that backoff counter is being reset when the connection is initially opened, even if the connection is immediately closed for being over capacity. These way these interact gives a spectacular failure case. Say that there's 2000 people playing a project with cloud variables. Let's say that about 100 of them are let in. The other 1900 clients will be constantly opening connections, trying to get in. Those numbers are made up but it should illustrate the problem. To fix this we'll wait a bit before resetting the backoff counter, to make sure that the server didn't refuse the connection. There are cleaner ways to fix this problem, but this solution doesn't require protocol changes.
1 parent 857dc10 commit 5e923f3

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/lib/cloud-provider.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class CloudProvider {
2626
this.cloudHost = cloudHost;
2727

2828
this.connectionAttempts = 0;
29+
this._resetConnectionAttemptsTimeout = null;
2930

3031
// A queue of messages to send which were received before the
3132
// connection was ready
@@ -81,9 +82,11 @@ class CloudProvider {
8182
}
8283

8384
onOpen () {
84-
// Reset connection attempts to 1 to make sure any subsequent reconnects
85-
// use connectionAttempts=1 to calculate timeout
86-
this.connectionAttempts = 1;
85+
this._resetConnectionAttemptsTimeout = setTimeout(() => {
86+
this.connectionAttempts = 0;
87+
this._resetConnectionAttemptsTimeout = null;
88+
}, 10 * 1000);
89+
8790
this.writeToServer('handshake');
8891
log.info(`Successfully connected to clouddata server.`);
8992

@@ -97,6 +100,11 @@ class CloudProvider {
97100
}
98101

99102
onClose (e) {
103+
if (this._resetConnectionAttemptsTimeout) {
104+
clearTimeout(this._resetConnectionAttemptsTimeout);
105+
this._resetConnectionAttemptsTimeout = null;
106+
}
107+
100108
// tw: code 4002 is "Username Error" -- do not try to reconnect
101109
if (e && e.code === 4002) {
102110
log.info('Cloud username is invalid. Not reconnecting.');
@@ -251,6 +259,10 @@ class CloudProvider {
251259
clearTimeout(this._connectionTimeout);
252260
this._connectionTimeout = null;
253261
}
262+
if (this._resetConnectionAttemptsTimeout) {
263+
clearTimeout(this._resetConnectionAttemptsTimeout);
264+
this._resetConnectionAttemptsTimeout = null;
265+
}
254266
this.connectionAttempts = 0;
255267
}
256268

0 commit comments

Comments
 (0)