Use TCP keepalive probes to detect local log stream closing

Change-type: patch
Closes: #1219
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2019-05-13 11:51:41 +01:00
parent ff8d784582
commit 96c975d17e
No known key found for this signature in database
GPG Key ID: 49690ED87032539F
3 changed files with 21 additions and 2 deletions

View File

@ -16,6 +16,8 @@
*/
import * as Bluebird from 'bluebird';
import * as _ from 'lodash';
import { NodeJSSocketWithFileDescriptor } from 'net-keepalive';
import * as os from 'os';
import * as request from 'request';
import * as Stream from 'stream';
@ -175,7 +177,7 @@ export class DeviceAPI {
return new Bluebird((resolve, reject) => {
const req = request.get(url);
req.on('error', reject).on('response', res => {
req.on('error', reject).on('response', async res => {
if (res.statusCode !== 200) {
reject(
new ApiErrors.DeviceAPIError(
@ -183,6 +185,18 @@ export class DeviceAPI {
),
);
}
res.socket.setKeepAlive(true, 1000);
if (os.platform() !== 'win32') {
const NetKeepalive = await import('net-keepalive');
// Certain versions of typescript won't convert
// this automatically
const sock = (res.socket as any) as NodeJSSocketWithFileDescriptor;
// We send a tcp keepalive probe once every 5 seconds
NetKeepalive.setKeepAliveInterval(sock, 5000);
// After 5 failed probes, the connection is marked as
// closed
NetKeepalive.setKeepAliveProbes(sock, 5);
}
resolve(res);
});
});

View File

@ -45,7 +45,10 @@ export function displayDeviceLogs(
});
logs.on('error', reject);
logs.on('end', resolve);
logs.on('end', () => {
logger.logError('Connection to device lost');
resolve();
});
});
}

View File

@ -74,6 +74,7 @@
"@types/lodash": "4.14.112",
"@types/mixpanel": "2.14.0",
"@types/mkdirp": "0.5.2",
"@types/net-keepalive": "^0.4.0",
"@types/node": "6.14.2",
"@types/prettyjson": "0.0.28",
"@types/raven": "2.5.1",
@ -186,6 +187,7 @@
"window-size": "^1.1.0"
},
"optionalDependencies": {
"net-keepalive": "^1.2.1",
"removedrive": "^1.0.0",
"windosu": "^0.2.0"
}