mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-01-18 02:39:49 +00:00
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:
parent
ff8d784582
commit
96c975d17e
@ -16,6 +16,8 @@
|
|||||||
*/
|
*/
|
||||||
import * as Bluebird from 'bluebird';
|
import * as Bluebird from 'bluebird';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
import { NodeJSSocketWithFileDescriptor } from 'net-keepalive';
|
||||||
|
import * as os from 'os';
|
||||||
import * as request from 'request';
|
import * as request from 'request';
|
||||||
import * as Stream from 'stream';
|
import * as Stream from 'stream';
|
||||||
|
|
||||||
@ -175,7 +177,7 @@ export class DeviceAPI {
|
|||||||
return new Bluebird((resolve, reject) => {
|
return new Bluebird((resolve, reject) => {
|
||||||
const req = request.get(url);
|
const req = request.get(url);
|
||||||
|
|
||||||
req.on('error', reject).on('response', res => {
|
req.on('error', reject).on('response', async res => {
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200) {
|
||||||
reject(
|
reject(
|
||||||
new ApiErrors.DeviceAPIError(
|
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);
|
resolve(res);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -45,7 +45,10 @@ export function displayDeviceLogs(
|
|||||||
});
|
});
|
||||||
|
|
||||||
logs.on('error', reject);
|
logs.on('error', reject);
|
||||||
logs.on('end', resolve);
|
logs.on('end', () => {
|
||||||
|
logger.logError('Connection to device lost');
|
||||||
|
resolve();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -74,6 +74,7 @@
|
|||||||
"@types/lodash": "4.14.112",
|
"@types/lodash": "4.14.112",
|
||||||
"@types/mixpanel": "2.14.0",
|
"@types/mixpanel": "2.14.0",
|
||||||
"@types/mkdirp": "0.5.2",
|
"@types/mkdirp": "0.5.2",
|
||||||
|
"@types/net-keepalive": "^0.4.0",
|
||||||
"@types/node": "6.14.2",
|
"@types/node": "6.14.2",
|
||||||
"@types/prettyjson": "0.0.28",
|
"@types/prettyjson": "0.0.28",
|
||||||
"@types/raven": "2.5.1",
|
"@types/raven": "2.5.1",
|
||||||
@ -186,6 +187,7 @@
|
|||||||
"window-size": "^1.1.0"
|
"window-size": "^1.1.0"
|
||||||
},
|
},
|
||||||
"optionalDependencies": {
|
"optionalDependencies": {
|
||||||
|
"net-keepalive": "^1.2.1",
|
||||||
"removedrive": "^1.0.0",
|
"removedrive": "^1.0.0",
|
||||||
"windosu": "^0.2.0"
|
"windosu": "^0.2.0"
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user