Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nasa/openmct.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Mazzella <ozyx@users.noreply.github.com>2022-10-06 22:41:38 +0300
committerGitHub <noreply@github.com>2022-10-06 22:41:38 +0300
commit866859a9375a454db8656735eb588a6214a2c835 (patch)
tree49318ef4498312a47dc0635631df894bb0d81d4d
parentafc54f41f6cdc68fa9d44ee04a60a7958470ee18 (diff)
[CouchDB] Re-establish feed connection if EventSource is closed due to error (#5845)
* Re-establish feed connection if EventSource is closed due to error * Use keepAlive timer * Get rid of magic numbers and add comment
-rw-r--r--src/plugins/persistence/couch/CouchChangesFeed.js37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/plugins/persistence/couch/CouchChangesFeed.js b/src/plugins/persistence/couch/CouchChangesFeed.js
index 3c1445cec..4547c6c9e 100644
--- a/src/plugins/persistence/couch/CouchChangesFeed.js
+++ b/src/plugins/persistence/couch/CouchChangesFeed.js
@@ -2,6 +2,9 @@
const connections = [];
let connected = false;
let couchEventSource;
+ let changesFeedUrl;
+ const keepAliveTime = 20 * 1000;
+ let keepAliveTimer;
const controller = new AbortController();
self.onconnect = function (e) {
@@ -35,7 +38,8 @@
return;
}
- self.listenForChanges(event.data.url);
+ changesFeedUrl = event.data.url;
+ self.listenForChanges();
}
};
@@ -63,17 +67,28 @@
});
};
- self.listenForChanges = function (url) {
- console.debug('⇿ Opening CouchDB change feed connection ⇿');
-
- couchEventSource = new EventSource(url);
- couchEventSource.onerror = self.onerror;
- couchEventSource.onopen = self.onopen;
+ self.listenForChanges = function () {
+ if (keepAliveTimer) {
+ clearTimeout(keepAliveTimer);
+ }
- // start listening for events
- couchEventSource.addEventListener('message', self.onCouchMessage);
- connected = true;
- console.debug('⇿ Opened connection ⇿');
+ /**
+ * Once the connection has been opened, poll every 20 seconds to see if the EventSource has closed unexpectedly.
+ * If it has, attempt to reconnect.
+ */
+ keepAliveTimer = setTimeout(self.listenForChanges, keepAliveTime);
+
+ if (!couchEventSource || couchEventSource.readyState === EventSource.CLOSED) {
+ console.debug('⇿ Opening CouchDB change feed connection ⇿');
+ couchEventSource = new EventSource(changesFeedUrl);
+ couchEventSource.onerror = self.onerror;
+ couchEventSource.onopen = self.onopen;
+
+ // start listening for events
+ couchEventSource.addEventListener('message', self.onCouchMessage);
+ connected = true;
+ console.debug('⇿ Opened connection ⇿');
+ }
};
self.updateCouchStateIndicator = function () {