Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

## Enhancements

* [phoenix.js] Long polling retries interrupted requests without rejoining
channels while its server session remains alive. Messages are retained until
acknowledged and retried pushes are deduplicated. New `socket.onSuspend` and
`socket.onResume` callbacks report interruptions and recovery without a
channel error. Older servers retain the existing reconnect behavior. During
a rolling deploy, a session that reaches a node with an older server falls
back to that behavior for the rest of the session.
* The longpoll session token is now sent in a `x-phoenix-longpoll-token`
header instead of the query string. If you do rolling deploys where both
old and new nodes are active at the same time, ensure that you deploy
Expand Down
3 changes: 3 additions & 0 deletions assets/js/phoenix/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ export default class Connection {
this.channelsErrored = false
// set once the close event was handled, as LongPoll can emit it more than once
this.closeHandled = false
this.suspended = false
transport.onopen = () => { if(this.isCurrent()){ socket.onConnOpen() } }
transport.onerror = error => { if(this.isCurrent()){ socket.onConnError(error) } }
transport.onmessage = event => { if(this.isCurrent()){ socket.onConnMessage(event) } }
transport.onsuspend = () => { if(this.isCurrent()){ socket.onConnSuspend(this) } }
transport.onresume = () => { if(this.isCurrent()){ socket.onConnResume(this) } }
transport.onclose = event => {
if(this.isCurrent() && !this.closeHandled){
this.closeHandled = true
Expand Down
16 changes: 16 additions & 0 deletions assets/js/phoenix/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@
* socket.onClose( () => console.log("the connection dropped") )
* ```
*
* LongPoll automatically retries interrupted requests while the server keeps
* its session alive. During recovery, channels stay joined and outgoing messages
* are buffered. Use `onSuspend` and `onResume` to show connection status without
* resetting channel state:
*
* ```
* socket.onSuspend( () => console.log("recovering the connection") )
* socket.onResume( () => console.log("the connection recovered") )
* ```
*
* Recovery requires a server that supports acknowledged long-poll batches.
* With an older server, and for the rest of a session that reached a node
* with an older server during a rolling upgrade, failed requests close the
* socket and channels rejoin. If the session expires, the usual error/close events
* fire and channels rejoin. Push timeouts continue to apply while suspended.
*
*
* ## Channel Hooks
*
Expand Down
169 changes: 146 additions & 23 deletions assets/js/phoenix/longpoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {

import Ajax from "./ajax"

// how long we wait before retrying a failed request of a session we can resume
const RETRY_DELAYS = [100, 500, 1000, 2000, 5000]

let arrayBufferToBase64 = (buffer) => {
let binary = ""
let bytes = new Uint8Array(buffer)
Expand All @@ -33,10 +36,23 @@ export default class LongPoll {
this.currentBatch = null
this.currentBatchTimer = null
this.batchBuffer = []
// set once the server tells us that it keeps the messages until we acknowledge them,
// so we can retry a failed request instead of starting a new session
this.resumable = false
this.degraded = false
// the last batch we received, and the seq of the last message we sent
this.ack = 0
this.seq = 0
this.retries = {GET: 0, POST: 0}
this.retryTimers = new Set()
// set once a retry failed as well, until GET and POST have both recovered
this.interrupted = false
this.onopen = function (){ } // noop
this.onerror = function (){ } // noop
this.onmessage = function (){ } // noop
this.onclose = function (){ } // noop
this.onsuspend = function (){ } // noop
this.onresume = function (){ } // noop
this.pollEndpoint = this.normalizeEndpoint(endPoint)
this.readyState = SOCKET_STATES.connecting
// we must wait for the caller to finish setting up our callbacks and timeout properties,
Expand All @@ -51,8 +67,8 @@ export default class LongPoll {
.replace(new RegExp("(.*)\/" + TRANSPORTS.websocket), "$1/" + TRANSPORTS.longpoll))
}

endpointURL(){
return this.pollEndpoint
endpointURL(params = {}){
return Ajax.appendParams(this.pollEndpoint, params)
}

closeAndRetry(error, code, reason, wasClean){
Expand All @@ -79,22 +95,36 @@ export default class LongPoll {
if(this.authToken){
headers["X-Phoenix-AuthToken"] = this.authToken
}
this.ajax("GET", headers, null, () => this.ontimeout(), resp => {
const params = this.resumable ? {ack: this.ack} : {}
this.ajax("GET", headers, null, params, () => this.retryOr("GET", () => this.poll(), () => this.ontimeout()), resp => {
if(resp){
var {status, token, messages} = resp
var {status, token, messages, batch} = resp
if(status === 410 && this.token !== null){
// In case we already have a token, this means that our existing session
// is gone. We fail so that the client rejoins its channels.
this.closeAndRetry(410, 3410, "session_gone", false)
return
}
this.token = token
if(batch !== undefined){
this.resumable = !this.degraded
} else if(this.resumable && (status === 200 || status === 204)){
// TODO: Remove in Phoenix 1.10, when no cluster mixes 1.8 and newer nodes.
//
// The poll went through a node with an older server, as during a rolling
// deploy, which forwards it without our ack. The session then dropped the
// messages we did not acknowledge yet, like it does for older clients.
// We got them unless this poll is a retry, as they were in the response we lost.
if(!this.degrade()){ return }
}
} else {
status = 0
}

switch(status){
case 200:
// A late poll may send a batch we already received. Batches are immutable.
if(batch !== undefined && batch <= this.ack){ messages = [] }
if(batch !== undefined){ this.ack = Math.max(this.ack, batch) }
messages.forEach(msg => {
// Tasks are what things like event handlers, setTimeout callbacks,
// promise resolves and more are run within.
Expand All @@ -117,15 +147,17 @@ export default class LongPoll {
//
// We might stop before that, e.g. when a POST times out or a message
// handler disconnects, which cancels the remaining messages.
const timer = setTimeout(() => {
this.messageTimers.delete(timer)
this.onmessage({data: msg})
}, 0)
this.messageTimers.add(timer)
this.scheduleMessage(() => this.onmessage({data: msg}))
})
// Restore only after delivering the batch, so hooks see its updates.
// Closing from a message callback cancels this task as well.
this.scheduleMessage(() => {
this.succeeded("GET")
this.poll()
})
this.poll()
break
case 204:
this.succeeded("GET")
this.poll()
break
case 410:
Expand All @@ -138,6 +170,9 @@ export default class LongPoll {
this.close(1008, "forbidden", false)
break
case 0:
// the connection failed, but the server still has our session and messages
this.retryOr("GET", () => this.poll(), () => this.closeAndRetry(500, 1011, "internal server error", false))
break
case 500:
this.closeAndRetry(500, 1011, "internal server error", false)
break
Expand All @@ -146,44 +181,129 @@ export default class LongPoll {
})
}

scheduleMessage(callback){
const timer = setTimeout(() => {
this.messageTimers.delete(timer)
callback()
}, 0)
this.messageTimers.add(timer)
}

// we collect all pushes within the current event loop by
// setTimeout 0, which optimizes back-to-back procedural
// pushes against an empty buffer

send(body){
if(typeof(body) !== "string"){ body = arrayBufferToBase64(body) }
// messages are numbered, so the server drops the ones it received when we send
// them again. A batch is sent with the seq of its first message.
this.seq++
if(this.currentBatch){
this.currentBatch.push(body)
} else if(this.awaitingBatchAck){
if(this.batchBuffer.length === 0){ this.batchBufferSeq = this.seq }
this.batchBuffer.push(body)
} else {
this.currentBatch = [body]
const seq = this.seq
this.currentBatchTimer = setTimeout(() => {
this.batchSend(this.currentBatch)
this.batchSend(this.currentBatch, seq)
this.currentBatch = null
}, 0)
}
}

batchSend(messages, offset = 0){
batchSend(messages, seq, offset = 0){
if(!this.isActive()){ return }
this.awaitingBatchAck = true
const next = offset + MAX_LONGPOLL_BATCH_SIZE
const batch = messages.slice(offset, next)
this.ajax("POST", {"Content-Type": "application/x-ndjson"}, batch.join("\n"), () => this.ontimeout(), resp => {
if(!resp || resp.status !== 200){
const retry = () => this.batchSend(messages, seq, offset)
// servers that don't announce batches don't know about seq either
const params = this.resumable ? {seq: seq + offset} : {}
this.ajax("POST", {"Content-Type": "application/x-ndjson"}, batch.join("\n"), params, () => this.retryOr("POST", retry, () => this.ontimeout()), resp => {
if(!resp){
this.retryOr("POST", retry, () => this.closeAndRetry(null, 1011, "internal server error", false))
} else if(resp.status !== 200){
// this calls stop which already clears awaitingBatchAck
this.closeAndRetry(resp && resp.status, 1011, "internal server error", false)
} else if(next < messages.length){
this.batchSend(messages, next)
} else if(this.batchBuffer.length > 0){
this.batchSend(this.batchBuffer)
this.batchBuffer = []
} else {
this.awaitingBatchAck = false
// a node with an older server forwards the request without our seq, so the
// session does not drop the messages we send again
//
// TODO: Remove in Phoenix 1.10, together with degrade()
if(this.resumable && resp.seq === undefined && !this.degrade()){ return }
this.succeeded("POST")
if(next < messages.length){
this.batchSend(messages, seq, next)
} else if(this.batchBuffer.length > 0){
this.batchSend(this.batchBuffer, this.batchBufferSeq)
this.batchBuffer = []
} else {
this.awaitingBatchAck = false
}
}
})
}

/**
* @private
*
* Stops resuming for the rest of the session. A node with an older server, as
* during a rolling deploy, does not forward our ack and seq. Retrying through it
* could lose or duplicate messages, so we fail like we do with an older server.
*
* TODO: Remove in Phoenix 1.10, when no cluster mixes 1.8 and newer nodes.
*/
degrade(){
this.degraded = true
this.resumable = false
// A request already being retried may have been processed. Neither replaying
// it without deduplication nor accepting a gap in received messages is safe.
if(this.retries.GET > 0 || this.retries.POST > 0){
this.closeAndRetry(0, 1011, "session no longer resumable", false)
return false
}
return true
}

/**
* @private
*
* Retries a failed request with backoff when the server keeps our session,
* otherwise fails.
*/
retryOr(method, retry, fail){
if(!this.resumable){ return fail() }
const delay = RETRY_DELAYS[Math.min(this.retries[method], RETRY_DELAYS.length - 1)]
this.retries[method]++
// a single failed request is retried quickly, so we only report the interruption
// once its retry failed as well
if(this.retries[method] > 1 && !this.interrupted){
this.interrupted = true
this.onsuspend()
}
if(!this.isActive()){ return }
const timer = setTimeout(() => {
this.retryTimers.delete(timer)
if(!this.resumable){ return fail() }
retry()
}, delay)
this.retryTimers.add(timer)
}

/**
* @private
*/
succeeded(method){
this.retries[method] = 0
// A successful poll must not hide an outstanding failed POST, or vice versa.
if(this.interrupted && this.retries.GET === 0 && this.retries.POST === 0){
this.interrupted = false
this.onresume()
}
}

close(code, reason, wasClean){
this.stop(SOCKET_STATES.closed)
this.emitClose(code, reason, wasClean)
Expand All @@ -201,6 +321,9 @@ export default class LongPoll {
for(let {req} of reqs){ req && req.abort() }
for(let timer of this.messageTimers){ clearTimeout(timer) }
this.messageTimers.clear()
for(let timer of this.retryTimers){ clearTimeout(timer) }
this.retryTimers.clear()
this.interrupted = false
this.batchBuffer = []
this.awaitingBatchAck = false
clearTimeout(this.currentBatchTimer)
Expand All @@ -221,7 +344,7 @@ export default class LongPoll {
}
}

ajax(method, headers, body, onCallerTimeout, callback){
ajax(method, headers, body, params, onCallerTimeout, callback){
// the request is ours until it completes or we stop, and the callbacks of a request
// that is not ours anymore are ignored. It is ours before it starts, as a request can
// complete while it starts.
Expand All @@ -233,7 +356,7 @@ export default class LongPoll {
if(this.token !== null){
headers = Object.assign({}, headers, {"X-Phoenix-Longpoll-Token": this.token})
}
request.req = Ajax.request(method, this.endpointURL(), headers, body, this.timeout, ontimeout, resp => {
request.req = Ajax.request(method, this.endpointURL(params), headers, body, this.timeout, ontimeout, resp => {
if(this.reqs.delete(request)){ callback(resp) }
})
}
Expand Down
Loading
Loading