Skip to content

Commit

Permalink
feat(realtime): Allows for immediate reconnect
Browse files Browse the repository at this point in the history
This change allows the realtime socket to try an immediate
reconnection for some events. This avoid waiting seconds or
minutes when recovering from an offline situation.
  • Loading branch information
edas committed Feb 27, 2020
1 parent d92102f commit a2170a8
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 18 deletions.
29 changes: 15 additions & 14 deletions packages/cozy-realtime/src/CozyRealtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,31 @@ class CozyRealtime {
* Connects a new websocket as soon as possible
* @private
*/
async connect() {
async connect({ immediate = false } = {}) {
logger.info('connecting…')
// avoid multiple concurrent calls to connect, keeps the first one
if (this.waitingForConnect) {
logger.debug('Pending reconnection, skipping reconnect')
return
}
logger.debug('No pending reconnection, will reconnect')
try {
this.waitingForConnect = true
await this.retryManager.waitBeforeNextAttempt()
this.createSocket()
} finally {
this.waitingForConnect = false
if (immediate) this.retryManager.stopCurrentAttemptWaitingTime()
} else {
logger.debug('No pending reconnection, will reconnect')
try {
this.waitingForConnect = true
if (!immediate) await this.retryManager.waitBeforeNextAttempt()
this.createSocket()
} finally {
this.waitingForConnect = false
}
}
}

/**
* Throws the previous socket and connect a new one
* @private
*/
async reconnect() {
async reconnect({ immediate = false } = {}) {
if (this.hasWebSocket()) this.revokeWebSocket()
this.connect()
this.connect({ immediate })
}

/**
Expand Down Expand Up @@ -602,7 +603,7 @@ class CozyRealtime {
* @private
*/
onResume() {
this.reconnect()
this.reconnect({ immediate: true })
}

/* * * * * * * * * * * * * * * */
Expand Down Expand Up @@ -637,7 +638,7 @@ class CozyRealtime {
*/
onOnline() {
logger.info('reconnect because receiving an online event')
this.reconnect()
this.reconnect({ immediate: true })
}

/**
Expand Down
43 changes: 42 additions & 1 deletion packages/cozy-realtime/src/CozyRealtime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,55 @@ describe('CozyRealtime', () => {
realtime.retryManager.onFailure()
realtime.retryManager.onFailure()
realtime.retryManager.onFailure()
realtime.onOnline()
realtime.onWebSocketError()
await sleep(25)

// reconnect before the previous connection finishes to wait
realtime.onOnline()
await realtime.waitForSocketReady()

expect(server.onconnect).toHaveBeenCalledTimes(2)
realtime.unsubscribeAll()
server.stop(done)
})
})

describe('on becoming online', () => {
it('reconnects', async done => {
const server = createSocketServer()
server.onconnect.named = 'reconnect'
const realtime = createRealtime()
const handler = jest.fn()
realtime.subscribe(event, type, undefined, handler)
await realtime.waitForSocketReady()

const event = new Event('online')
window.dispatchEvent(event)

await sleep(100)
await realtime.waitForSocketReady()
expect(server.onconnect).toHaveBeenCalledTimes(2)
realtime.unsubscribeAll()
server.stop(done)
})

it('reconnects immediatly', async done => {
const server = createSocketServer()
const realtime = createRealtime()
const handler = jest.fn()
realtime.subscribe(event, type, undefined, handler)
await realtime.waitForSocketReady()

realtime.retryManager.onFailure()
realtime.retryManager.onFailure()
realtime.retryManager.onFailure()
realtime.retryManager.onFailure()
const event = new Event('online')
window.dispatchEvent(event)

await sleep(100)
expect(server.onconnect).toHaveBeenCalledTimes(2)
realtime.unsubscribeAll()
server.stop(done)
})
})
Expand Down
26 changes: 23 additions & 3 deletions packages/cozy-realtime/src/RetryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,31 @@ class RetryManager {
async waitBeforeNextAttempt() {
logger.debug('waitBeforeNextAttempt', this.wait)
if (this.wait) {
await new Promise(resolve => {
global.setTimeout(resolve, this.wait)
})
if (!this.waiting) {
let stop
const promise = new Promise(resolve => {
stop = () => {
// only remove the waiting status if it's still
// the current promise that is waiting and not a new one
if (this.waiting === promise) this.waiting = null
resolve()
}
global.setTimeout(stop, this.wait)
})
promise.stop = stop
this.waiting = promise
}
await this.waiting
}
}

/**
* If an attempt is waiting, stop the wait and retry now
* @see waitBeforeNextAttempt()
*/
stopCurrentAttemptWaitingTime() {
if (this.waiting) this.waiting.stop()
}
}

MicroEE.mixin(RetryManager)
Expand Down
18 changes: 18 additions & 0 deletions packages/cozy-realtime/src/RetryManager.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,22 @@ describe('RetryManager', () => {
expect(retry.shouldEmitError()).toBeFalsy()
})
})

describe('stopCurrentAttemptWaitingTime', () => {
it('stop the waiting time', async () => {
const handler = jest.fn()
const retry = new RetryManager()
retry.onFailure()
retry.onFailure()
retry.onFailure()
retry.onFailure()
retry.onFailure()
retry.onFailure()
retry.onFailure()
retry.waitBeforeNextAttempt().then(handler)
retry.stopCurrentAttemptWaitingTime()
await sleep(25)
expect(handler).toHaveBeenCalled()
})
})
})

0 comments on commit a2170a8

Please sign in to comment.