|
1 | 1 | import _ from 'underscore'
|
2 |
| -import {NylasLongConnection, DatabaseStore} from 'nylas-exports' |
3 |
| - |
4 |
| -class DeltaStreamingConnection extends NylasLongConnection { |
5 |
| - constructor(api, accountId, opts = {}) { |
6 |
| - // TODO FYI this whole class is changing in an upcoming diff |
7 |
| - opts.api = api |
8 |
| - opts.accountId = accountId |
9 |
| - opts.throttleResultsInterval = 1000 |
10 |
| - opts.closeIfDataStopsInterval = 15 * 1000 |
11 |
| - |
12 |
| - // Update cursor when deltas received |
13 |
| - opts.onResuls = (deltas = []) => { |
14 |
| - if (opts.onDeltas) opts.onDeltas(deltas, {source: "n1Cloud"}); |
15 |
| - const last = _.last(deltas); |
16 |
| - if (last && last.cursor) { |
17 |
| - this._setCursor(last.cursor) |
| 2 | +import {ExponentialBackoffScheduler} from 'isomorphic-core' |
| 3 | +import { |
| 4 | + Actions, |
| 5 | + Account, |
| 6 | + APIError, |
| 7 | + N1CloudAPI, |
| 8 | + DatabaseStore, |
| 9 | + OnlineStatusStore, |
| 10 | + NylasLongConnection, |
| 11 | +} from 'nylas-exports'; |
| 12 | +import DeltaProcessor from './delta-processor' |
| 13 | + |
| 14 | + |
| 15 | +const MAX_RETRY_DELAY = 5 * 60 * 1000; // 5 minutes |
| 16 | +const BASE_RETRY_DELAY = 1000; |
| 17 | + |
| 18 | +class DeltaStreamingConnection { |
| 19 | + constructor(account) { |
| 20 | + this._account = account |
| 21 | + this._state = {cursor: null, status: null} |
| 22 | + this._longConnection = null |
| 23 | + this._writeStateDebounced = _.debounce(this._writeState, 100) |
| 24 | + this._unsubscribers = [] |
| 25 | + this._backoffScheduler = new ExponentialBackoffScheduler({ |
| 26 | + baseDelay: BASE_RETRY_DELAY, |
| 27 | + maxDelay: MAX_RETRY_DELAY, |
| 28 | + }) |
| 29 | + |
| 30 | + this._setupListeners() |
| 31 | + NylasEnv.onBeforeUnload = (readyToUnload) => { |
| 32 | + this._writeState().finally(readyToUnload) |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + start() { |
| 37 | + try { |
| 38 | + const {cursor = 0} = this._state |
| 39 | + this._longConnection = new NylasLongConnection({ |
| 40 | + api: N1CloudAPI, |
| 41 | + accountId: this._account.id, |
| 42 | + path: `/delta/streaming?cursor=${cursor}`, |
| 43 | + throttleResultsInterval: 1000, |
| 44 | + closeIfDataStopsInterval: 15 * 1000, |
| 45 | + onError: this._onError, |
| 46 | + onResults: this._onResults, |
| 47 | + onStatusChanged: this._onStatusChanged, |
| 48 | + }) |
| 49 | + this._longConnection.start() |
| 50 | + } catch (err) { |
| 51 | + this._onError(err) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + restart() { |
| 56 | + try { |
| 57 | + this._restarting = true |
| 58 | + this.close(); |
| 59 | + this._disposeListeners() |
| 60 | + this._setupListeners() |
| 61 | + this.start(); |
| 62 | + } finally { |
| 63 | + this._restarting = false |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + close() { |
| 68 | + this._disposeListeners() |
| 69 | + this._longConnection.close() |
| 70 | + } |
| 71 | + |
| 72 | + end() { |
| 73 | + this._disposeListeners() |
| 74 | + this._longConnection.end() |
| 75 | + } |
| 76 | + |
| 77 | + async loadStateFromDatabase() { |
| 78 | + let json = await DatabaseStore.findJSONBlob(`DeltaStreamingConnectionStatus:${this._account.id}`) |
| 79 | + |
| 80 | + if (!json) { |
| 81 | + // Migrate from old storage key |
| 82 | + const oldState = await DatabaseStore.findJSONBlob(`NylasSyncWorker:${this._account.id}`) |
| 83 | + if (!oldState) { return; } |
| 84 | + const {deltaCursors = {}, deltaStatus = {}} = oldState |
| 85 | + json = { |
| 86 | + cursor: deltaCursors.n1Cloud || null, |
| 87 | + status: deltaStatus.n1Cloud || null, |
18 | 88 | }
|
19 | 89 | }
|
20 |
| - super(opts) |
21 | 90 |
|
22 |
| - this._onError = opts.onError || (() => {}) |
| 91 | + if (!json) { return } |
| 92 | + this._state = json; |
| 93 | + } |
23 | 94 |
|
24 |
| - const {getCursor, setCursor} = opts |
25 |
| - this._getCursor = getCursor |
26 |
| - this._setCursor = setCursor |
| 95 | + _setupListeners() { |
| 96 | + this._unsubscribers = [ |
| 97 | + Actions.retryDeltaConnection.listen(this.restart, this), |
| 98 | + OnlineStatusStore.listen(this._onOnlineStatusChanged, this), |
| 99 | + ] |
27 | 100 | }
|
28 | 101 |
|
29 |
| - _deltaStreamingPath(cursor) { |
30 |
| - return `/delta/streaming?cursor=${cursor}` |
| 102 | + _disposeListeners() { |
| 103 | + this._unsubscribers.forEach(usub => usub()) |
| 104 | + this._unsubscribers = [] |
31 | 105 | }
|
32 | 106 |
|
33 |
| - onError(err = {}) { |
| 107 | + _writeState() { |
| 108 | + return DatabaseStore.inTransaction(t => |
| 109 | + t.persistJSONBlob(`DeltaStreamingConnectionStatus:${this._account.id}`, this._state) |
| 110 | + ); |
| 111 | + } |
| 112 | + |
| 113 | + _setCursor = (cursor) => { |
| 114 | + this._state.cursor = cursor; |
| 115 | + this._writeStateDebounced(); |
| 116 | + } |
| 117 | + |
| 118 | + _onOnlineStatusChanged = () => { |
| 119 | + if (OnlineStatusStore.isOnline()) { |
| 120 | + this.restart() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + _onStatusChanged = (status) => { |
| 125 | + if (this._restarting) { return; } |
| 126 | + this._state.status = status; |
| 127 | + this._writeStateDebounced(); |
| 128 | + const {Closed, Connected} = NylasLongConnection.Status |
| 129 | + if (status === Connected) { |
| 130 | + this._backoffScheduler.reset() |
| 131 | + } |
| 132 | + if (status === Closed) { |
| 133 | + setTimeout(() => this.restart(), this._backoffScheduler.nextDelay()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + _onResults = (deltas = []) => { |
| 138 | + this._backoffScheduler.reset() |
| 139 | + |
| 140 | + const last = _.last(deltas); |
| 141 | + if (last && last.cursor) { |
| 142 | + this._setCursor(last.cursor) |
| 143 | + } |
| 144 | + DeltaProcessor.process(deltas, {source: 'n1Cloud'}) |
| 145 | + } |
| 146 | + |
| 147 | + _onError = (err = {}) => { |
34 | 148 | if (err.message && err.message.includes('Invalid cursor')) {
|
35 |
| - const error = new Error('Delta Connection: Cursor is invalid. Need to blow away local cache.'); |
| 149 | + // TODO is this still necessary? |
| 150 | + const error = new Error('DeltaStreamingConnection: Cursor is invalid. Need to blow away local cache.'); |
36 | 151 | NylasEnv.reportError(error)
|
37 | 152 | this._setCursor(0)
|
38 | 153 | DatabaseStore._handleSetupError(error)
|
| 154 | + return |
39 | 155 | }
|
40 |
| - this._onError(err) |
41 |
| - } |
42 | 156 |
|
43 |
| - start() { |
44 |
| - this._path = this._deltaStreamingPath(this._getCursor() || 0) |
45 |
| - super.start() |
| 157 | + if (err instanceof APIError && err.statusCode === 401) { |
| 158 | + Actions.updateAccount(this._account.id, { |
| 159 | + syncState: Account.SYNC_STATE_AUTH_FAILED, |
| 160 | + syncError: err.toJSON(), |
| 161 | + }) |
| 162 | + } |
| 163 | + |
| 164 | + err.message = `Error connecting to delta stream: ${err.message}` |
| 165 | + const ignorableStatusCodes = [ |
| 166 | + 0, // When errors like ETIMEDOUT, ECONNABORTED or ESOCKETTIMEDOUT occur from the client |
| 167 | + 404, // Don't report not-founds |
| 168 | + 408, // Timeout error code |
| 169 | + 429, // Too many requests |
| 170 | + ] |
| 171 | + if (!ignorableStatusCodes.includes(err.statusCode)) { |
| 172 | + NylasEnv.reportError(err) |
| 173 | + } |
| 174 | + this.close() |
| 175 | + |
| 176 | + setTimeout(() => this.restart(), this._backoffScheduler.nextDelay()); |
46 | 177 | }
|
47 | 178 | }
|
48 | 179 |
|
|
0 commit comments