Skip to content

Commit

Permalink
Only memorize lonpgoll fallback if primary transport never establishe…
Browse files Browse the repository at this point in the history
…d connection. Closes #5741
  • Loading branch information
chrismccord committed Mar 11, 2024
1 parent 76b6c3d commit b2eeccf
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
9 changes: 6 additions & 3 deletions assets/js/phoenix/socket.js
Expand Up @@ -115,6 +115,7 @@ export default class Socket {
this.ref = 0
this.timeout = opts.timeout || DEFAULT_TIMEOUT
this.transport = opts.transport || global.WebSocket || LongPoll
this.primaryPassedHealthCheck = false
this.longPollFallbackMs = opts.longPollFallbackMs
this.fallbackTimer = null
this.sessionStore = opts.sessionStorage || global.sessionStorage
Expand Down Expand Up @@ -366,11 +367,10 @@ export default class Socket {
this.log("transport", `falling back to ${fallbackTransport.name}...`, reason)
this.off([openRef, errorRef])
primaryTransport = false
this.storeSession("phx:longpoll", "true")
this.replaceTransport(fallbackTransport)
this.transportConnect()
}
if(this.getSession("phx:longpoll")){ return fallback("memorized") }
if(this.getSession(`phx:fallback:${fallbackTransport.name}`)){ return fallback("memorized") }

this.fallbackTimer = setTimeout(fallback, fallbackThreshold)

Expand All @@ -384,13 +384,16 @@ export default class Socket {
this.onOpen(() => {
established = true
if(!primaryTransport){
return console.log("transport", `established ${fallbackTransport.name} fallback`)
// only memorize LP if we never connected to primary
if(!this.primaryPassedHealthCheck){ this.storeSession(`phx:fallback:${fallbackTransport.name}`, "true") }
return this.log("transport", `established ${fallbackTransport.name} fallback`)
}
// if we've established primary, give the fallback a new period to attempt ping
clearTimeout(this.fallbackTimer)
this.fallbackTimer = setTimeout(fallback, fallbackThreshold)
this.ping(rtt => {
this.log("transport", "connected to primary after", rtt)
this.primaryPassedHealthCheck = true
clearTimeout(this.fallbackTimer)
})
})
Expand Down
10 changes: 7 additions & 3 deletions priv/static/phoenix.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions priv/static/phoenix.cjs.js.map

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions priv/static/phoenix.js
Expand Up @@ -1026,6 +1026,7 @@ var Phoenix = (() => {
this.ref = 0;
this.timeout = opts.timeout || DEFAULT_TIMEOUT;
this.transport = opts.transport || global.WebSocket || LongPoll;
this.primaryPassedHealthCheck = false;
this.longPollFallbackMs = opts.longPollFallbackMs;
this.fallbackTimer = null;
this.sessionStore = opts.sessionStorage || global.sessionStorage;
Expand Down Expand Up @@ -1282,11 +1283,10 @@ var Phoenix = (() => {
this.log("transport", `falling back to ${fallbackTransport.name}...`, reason);
this.off([openRef, errorRef]);
primaryTransport = false;
this.storeSession("phx:longpoll", "true");
this.replaceTransport(fallbackTransport);
this.transportConnect();
};
if (this.getSession("phx:longpoll")) {
if (this.getSession(`phx:fallback:${fallbackTransport.name}`)) {
return fallback("memorized");
}
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
Expand All @@ -1300,12 +1300,16 @@ var Phoenix = (() => {
this.onOpen(() => {
established = true;
if (!primaryTransport) {
return console.log("transport", `established ${fallbackTransport.name} fallback`);
if (!this.primaryPassedHealthCheck) {
this.storeSession(`phx:fallback:${fallbackTransport.name}`, "true");
}
return this.log("transport", `established ${fallbackTransport.name} fallback`);
}
clearTimeout(this.fallbackTimer);
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
this.ping((rtt) => {
this.log("transport", "connected to primary after", rtt);
this.primaryPassedHealthCheck = true;
clearTimeout(this.fallbackTimer);
});
});
Expand Down
4 changes: 2 additions & 2 deletions priv/static/phoenix.min.js

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions priv/static/phoenix.mjs
Expand Up @@ -997,6 +997,7 @@ var Socket = class {
this.ref = 0;
this.timeout = opts.timeout || DEFAULT_TIMEOUT;
this.transport = opts.transport || global.WebSocket || LongPoll;
this.primaryPassedHealthCheck = false;
this.longPollFallbackMs = opts.longPollFallbackMs;
this.fallbackTimer = null;
this.sessionStore = opts.sessionStorage || global.sessionStorage;
Expand Down Expand Up @@ -1253,11 +1254,10 @@ var Socket = class {
this.log("transport", `falling back to ${fallbackTransport.name}...`, reason);
this.off([openRef, errorRef]);
primaryTransport = false;
this.storeSession("phx:longpoll", "true");
this.replaceTransport(fallbackTransport);
this.transportConnect();
};
if (this.getSession("phx:longpoll")) {
if (this.getSession(`phx:fallback:${fallbackTransport.name}`)) {
return fallback("memorized");
}
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
Expand All @@ -1271,12 +1271,16 @@ var Socket = class {
this.onOpen(() => {
established = true;
if (!primaryTransport) {
return console.log("transport", `established ${fallbackTransport.name} fallback`);
if (!this.primaryPassedHealthCheck) {
this.storeSession(`phx:fallback:${fallbackTransport.name}`, "true");
}
return this.log("transport", `established ${fallbackTransport.name} fallback`);
}
clearTimeout(this.fallbackTimer);
this.fallbackTimer = setTimeout(fallback, fallbackThreshold);
this.ping((rtt) => {
this.log("transport", "connected to primary after", rtt);
this.primaryPassedHealthCheck = true;
clearTimeout(this.fallbackTimer);
});
});
Expand Down
4 changes: 2 additions & 2 deletions priv/static/phoenix.mjs.map

Large diffs are not rendered by default.

0 comments on commit b2eeccf

Please sign in to comment.