Skip to content

Commit

Permalink
[FIX JENKINS-42196] Eliminate overly-eager SSE ping request (#18)
Browse files Browse the repository at this point in the history
* Only ping after giving the connection a chance to heal itself

* 0.0.19-tf-JENKINS-42196-1
  • Loading branch information
tfennelly committed Feb 21, 2017
1 parent 2771559 commit c225bf9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@jenkins-cd/sse-gateway",
"version": "0.0.18",
"version": "0.0.19-tf-JENKINS-42196-1",
"description": "Client API for the Jenkins SSE Gateway plugin. Browser UI push events from Jenkins.",
"main": "src/main/js/index.js",
"files": [
Expand Down
52 changes: 31 additions & 21 deletions src/main/js/SSEConnection.js
Expand Up @@ -128,6 +128,10 @@ SSEConnection.prototype = {
var errorTracking = {
errors: [],
reset: function () {
if (errorTracking.waitForHealingTimeout) {
clearTimeout(errorTracking.waitForHealingTimeout);
delete errorTracking.waitForHealingTimeout;
}
if (errorTracking.pingbackTimeout) {
clearTimeout(errorTracking.pingbackTimeout);
delete errorTracking.pingbackTimeout;
Expand Down Expand Up @@ -170,28 +174,34 @@ SSEConnection.prototype = {
source.addEventListener('error', function (e) {
LOGGER.debug('SSE channel "error" event.', e);
if (errorTracking.errors.length === 0) {
// Send ping request.
// If the connection is ok, we should get a pingback ack and the above
// errorTracking.pingbackTimeout should get cleared etc.
// See 'pingback' below
errorTracking.pingbackTimeout = setTimeout(function () {
delete errorTracking.pingbackTimeout;
if (typeof sseConnection._onerror === 'function'
&& errorTracking.errors.length > 0) {
var errorToSend = errorTracking.errors[0];
errorTracking.reset();
try {
sseConnection._onerror(errorToSend);
} catch (error) {
LOGGER.error('SSEConnection "onError" event handler ' +
'threw unexpected error.', error);
}
} else {
errorTracking.reset();
// First give the connection a chance to heal itself.
errorTracking.waitForHealingTimeout = setTimeout(function () {
if (errorTracking.errors.length !== 0) {
// The connection is still not ok. Lets fire a ping request.
// If the connection becomes ok, we should get a pingback
// ack and the timeouts etc should get cleared etc.
// See 'pingback' below
errorTracking.pingbackTimeout = setTimeout(function () {
delete errorTracking.pingbackTimeout;
if (typeof sseConnection._onerror === 'function'
&& errorTracking.errors.length > 0) {
var errorToSend = errorTracking.errors[0];
errorTracking.reset();
try {
sseConnection._onerror(errorToSend);
} catch (error) {
LOGGER.error('SSEConnection "onError" event handler ' +
'threw unexpected error.', error);
}
} else {
errorTracking.reset();
}
}, 3000); // TODO: magic num ... what's realistic ?
ajax.get(sseConnection.pingUrl + '?dispatcherId=' +
encodeURIComponent(
sseConnection.jenkinsSessionInfo.dispatcherId));
}
}, 5000); // TODO: magic num ... what's realistic ?
ajax.get(sseConnection.pingUrl + '?dispatcherId=' +
encodeURIComponent(sseConnection.jenkinsSessionInfo.dispatcherId));
}, 4000); // TODO: magic num ... what's realistic ?
}
errorTracking.errors.push(e);
}, false);
Expand Down

0 comments on commit c225bf9

Please sign in to comment.