EditorWatchdogManager.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. EditorWatchdogManager is used for end-to-end checks of edits.
  3. The editor UI is backed by Ace and CodeMirrors, which in turn are connected
  4. to ShareJs documents in the frontend.
  5. Edits propagate from the editor to ShareJs and are send through socket.io
  6. and real-time to document-updater.
  7. In document-updater edits are integrated into the document history and
  8. a confirmation/rejection is sent back to the frontend.
  9. Along the way things can get lost.
  10. We have certain safe-guards in place, but are still getting occasional
  11. reports of lost edits.
  12. EditorWatchdogManager is implementing the basis for end-to-end checks on
  13. two levels:
  14. - local/ShareJsDoc: edits that pass-by a ShareJs document shall get
  15. acknowledged eventually.
  16. - global: any edits made in the editor shall get acknowledged eventually,
  17. independent for which ShareJs document (potentially none) sees it.
  18. How does this work?
  19. ===================
  20. The global check is using a global EditorWatchdogManager that is available
  21. via the angular factory 'ide'.
  22. Local/ShareJsDoc level checks will connect to the global instance.
  23. Each EditorWatchdogManager keeps track of the oldest un-acknowledged edit.
  24. When ever a ShareJs document receives an acknowledgement event, a local
  25. EditorWatchdogManager will see it and also notify the global instance about
  26. it.
  27. The next edit cycle will clear the oldest un-acknowledged timestamp in case
  28. a new ack has arrived, otherwise it will bark loud! via the timeout handler.
  29. Scenarios
  30. =========
  31. - User opens the CodeMirror editor
  32. - attach global check to new CM instance
  33. - detach Ace from the local EditorWatchdogManager
  34. - when the frontend attaches the CM instance to ShareJs, we also
  35. attach it to the local EditorWatchdogManager
  36. - the internal attach process writes the document content to the editor,
  37. which in turn emits 'change' events. These event need to be excluded
  38. from the watchdog. EditorWatchdogManager.ignoreEditsFor takes care
  39. of that.
  40. - User opens the Ace editor (again)
  41. - (attach global check to the Ace editor, only one copy of Ace is around)
  42. - detach local EditorWatchdogManager from CM
  43. - likewise with CM, attach Ace to the local EditorWatchdogManager
  44. - User makes an edit
  45. - the editor will emit a 'change' event
  46. - the global EditorWatchdogManager will process it first
  47. - the local EditorWatchdogManager will process it next
  48. - Document-updater confirms an edit
  49. - the local EditorWatchdogManager will process it first, it passes it on to
  50. - the global EditorWatchdogManager will process it next
  51. Time
  52. ====
  53. The delay between edits and acks is measured using a monotonic clock:
  54. `performance.now()`.
  55. It is agnostic to system clock changes in either direction and timezone
  56. changes do not affect it as well.
  57. Roughly speaking, it is initialized with `0` when the `window` context is
  58. created, before our JS app boots.
  59. As per canIUse.com and MDN `performance.now()` is available to all supported
  60. Browsers, including IE11.
  61. See also: https://caniuse.com/?search=performance.now
  62. See also: https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
  63. */
  64. // TIMEOUT specifies the timeout for edits into a single ShareJsDoc.
  65. const TIMEOUT = 60 * 1000
  66. // GLOBAL_TIMEOUT specifies the timeout for edits into any ShareJSDoc.
  67. const GLOBAL_TIMEOUT = TIMEOUT
  68. // REPORT_EVERY specifies how often we send events/report errors.
  69. const REPORT_EVERY = 60 * 1000
  70. export default class EditorWatchdogManager {
  71. constructor({ parent, onTimeoutHandler }) {
  72. this.timeout = parent ? TIMEOUT : GLOBAL_TIMEOUT
  73. this.parent = parent
  74. this.onTimeoutHandler =
  75. onTimeoutHandler || (parent && parent.onTimeoutHandler)
  76. this.lastAck = null
  77. this.lastUnackedEdit = null
  78. this.lastReport = null
  79. }
  80. onAck() {
  81. this.lastAck = performance.now()
  82. // bubble up to globalEditorWatchdogManager
  83. if (this.parent) this.parent.onAck()
  84. }
  85. onEdit() {
  86. // Use timestamps to track the high-water mark of unacked edits
  87. const now = performance.now()
  88. // Discard the last unacked edit if there are now newer acks
  89. if (this.lastAck > this.lastUnackedEdit) {
  90. this.lastUnackedEdit = null
  91. }
  92. // Start tracking for this keypress if we aren't already tracking an
  93. // unacked edit
  94. if (!this.lastUnackedEdit) {
  95. this.lastUnackedEdit = now
  96. }
  97. // Report an error if the last tracked edit hasn't been cleared by an
  98. // ack from the server after a long time
  99. const delay = now - this.lastUnackedEdit
  100. if (delay > this.timeout) {
  101. const timeOrigin = Date.now() - now
  102. const scope = this.parent ? 'ShareJsDoc' : 'global'
  103. const lastAck = new Date(this.lastAck ? timeOrigin + this.lastAck : 0)
  104. const lastUnackedEdit = new Date(timeOrigin + this.lastUnackedEdit)
  105. const meta = { scope, delay, lastAck, lastUnackedEdit }
  106. this._log('timedOut', meta)
  107. // do not flood the server with losing-edits events
  108. const reportedRecently = now - this.lastReport < REPORT_EVERY
  109. if (!reportedRecently) {
  110. this.lastReport = now
  111. if (this.parent) {
  112. // report this timeout once from the lowest level
  113. this.parent.lastReport = this.lastReport
  114. }
  115. this.onTimeoutHandler(meta)
  116. }
  117. }
  118. }
  119. attachToEditor(editorName, editor) {
  120. const onChangeFn = change => {
  121. // Ignore remote changes.
  122. if (change.origin !== 'remote') this.onEdit()
  123. }
  124. let onChange
  125. if (!this.parent) {
  126. // Change events are processed in sequence, starting with the listener
  127. // that attaches first.
  128. // The global watchdog attaches before the local one does.
  129. // We want to report bottom up in any case (ShareJs -> global), not just
  130. // for continuous edits (which the different timeouts achieved), but
  131. // also for delayed edits: a user leaves their desk, comes back after
  132. // 10min and edits again.
  133. // The global watchdog would see the edit first, potentially reporting a
  134. // missed ack attributed to a missing ShareJsDoc -- even tho a doc is
  135. // still active and listening for edits.
  136. // Delay the run of the global handler into the next event loop cycle.
  137. onChange = change => setTimeout(onChangeFn, 0, change)
  138. } else {
  139. onChange = onChangeFn
  140. }
  141. this._log('attach to editor', editorName)
  142. editor.on('change', onChange)
  143. const detachFromEditor = () => {
  144. this._log('detach from editor', editorName)
  145. editor.off('change', onChange)
  146. }
  147. return detachFromEditor
  148. }
  149. _log() {
  150. const scope = this.parent ? 'ShareJsDoc' : 'global'
  151. sl_console.log(`[EditorWatchdogManager] ${scope}:`, ...arguments)
  152. }
  153. }