Skip to content

Commit

Permalink
Add force keepalive using random generated client
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Jun 21, 2020
1 parent a9af906 commit 0ef73c8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 8 deletions.
56 changes: 52 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,15 @@ let argv = _yargs.default.command('$0', 'start nshd').command('addr', 'show addr
describe: 'show executed cmd in console',
type: 'boolean',
default: false
}).option('must-keepalive', {
describe: 'nshd will exit if keepalive failed',
type: 'boolean',
default: false
}).help('help').alias('h', 'help').wrap(Math.min(120, _yargs.default.terminalWidth())).argv;

const isWindows = _os.default.platform() === 'win32';
const pingInterval = 20000;
const forcePingInterval = 60000;
const ptyCols = 120;
const ptyRows = 30;
const sessionFlushIntervalInUse = 10;
Expand Down Expand Up @@ -218,6 +223,7 @@ function getAuthorizedUser(src) {
}

const client = new _nknSdk.default.MultiClient({
originalClient: true,
seed: wallet.getSeed(),
identifier: identifier
});
Expand Down Expand Up @@ -295,22 +301,64 @@ client.onConnect(() => {
}

let lastUpdateTime = new Date();
setInterval(async function () {
setInterval(async () => {
try {
await client.send(client.addr, '');
lastUpdateTime = new Date();
} catch (e) {
console.warn('Multiclient ping error:', e);
let t = new Date().getTime() - lastUpdateTime.getTime();

if (argv.mustKeepalive && t > pingInterval * 5) {
process.exit(1);
}

if (new Date().getTime() - lastUpdateTime.getTime() > pingInterval * 3) {
if (t > pingInterval * 3) {
console.log('Multiclient keepalive timeout, trying to reconnect...');

for (let c of Object.values(client.clients)) {
c.reconnect();
c._reconnect();
}
}
}
}, pingInterval);
let lastForceUpdateTime = new Date();
setInterval(async () => {
let tempIdentifier = _nknSdk.default.util.randomBytesHex(4);

if (identifier) {
tempIdentifier += '.' + identifier;
}

let tempClient = new _nknSdk.default.MultiClient({
originalClient: true,
seed: wallet.getSeed(),
identifier: tempIdentifier
});
tempClient.onConnect(() => {
setTimeout(async () => {
try {
await tempClient.send(client.addr, '');
lastForceUpdateTime = new Date();
} catch (e) {
console.warn('Force ping error:', e);
let t = new Date().getTime() - lastForceUpdateTime.getTime();

if (argv.mustKeepalive && t > forcePingInterval * 5) {
process.exit(1);
}

if (t > forcePingInterval * 3) {
console.log('Force keepalive timeout, trying to reconnect...');

for (let c of Object.values(client.clients)) {
c._reconnect();
}
}
}
}, 3000);
});
}, forcePingInterval);
});
client.onMessage(async ({
src,
Expand All @@ -323,7 +371,7 @@ client.onMessage(async ({
return false;
}

if (src === client.addr) {
if (src.endsWith(client.getPublicKey()) && !payload) {
return;
}

Expand Down
52 changes: 48 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,19 @@ let argv = yargs
type: 'boolean',
default: false,
})
.option('must-keepalive', {
describe: 'nshd will exit if keepalive failed',
type: 'boolean',
default: false,
})
.help('help')
.alias('h', 'help')
.wrap(Math.min(120, yargs.terminalWidth()))
.argv

const isWindows = os.platform() === 'win32'
const pingInterval = 20000
const forcePingInterval = 60000
const ptyCols = 120
const ptyRows = 30
const sessionFlushIntervalInUse = 10
Expand Down Expand Up @@ -200,6 +206,7 @@ function getAuthorizedUser(src) {
}

const client = new nkn.MultiClient({
originalClient: true,
seed: wallet.getSeed(),
identifier: identifier,
})
Expand Down Expand Up @@ -276,20 +283,57 @@ client.onConnect(() => {
}

let lastUpdateTime = new Date()
setInterval(async function () {
setInterval(async () => {
try {
await client.send(client.addr, '')
lastUpdateTime = new Date()
} catch (e) {
console.warn('Multiclient ping error:', e)
if (new Date().getTime() - lastUpdateTime.getTime() > pingInterval * 3) {
let t = new Date().getTime() - lastUpdateTime.getTime()
if (argv.mustKeepalive && t > pingInterval * 5) {
process.exit(1)
}
if (t > pingInterval * 3) {
console.log('Multiclient keepalive timeout, trying to reconnect...')
for (let c of Object.values(client.clients)) {
c.reconnect()
c._reconnect()
}
}
}
}, pingInterval)

let lastForceUpdateTime = new Date()
setInterval(async () => {
let tempIdentifier = nkn.util.randomBytesHex(4)
if (identifier) {
tempIdentifier += '.' + identifier
}
let tempClient = new nkn.MultiClient({
originalClient: true,
seed: wallet.getSeed(),
identifier: tempIdentifier,
});
tempClient.onConnect(() => {
setTimeout(async () => {
try {
await tempClient.send(client.addr, '')
lastForceUpdateTime = new Date()
} catch (e) {
console.warn('Force ping error:', e)
let t = new Date().getTime() - lastForceUpdateTime.getTime()
if (argv.mustKeepalive && t > forcePingInterval * 5) {
process.exit(1)
}
if (t > forcePingInterval * 3) {
console.log('Force keepalive timeout, trying to reconnect...')
for (let c of Object.values(client.clients)) {
c._reconnect()
}
}
}
}, 3000);
})
}, forcePingInterval)
})

client.onMessage(async ({ src, payload, payloadType, isEncrypted }) => {
Expand All @@ -298,7 +342,7 @@ client.onMessage(async ({ src, payload, payloadType, isEncrypted }) => {
return false
}

if (src === client.addr) {
if (src.endsWith(client.getPublicKey()) && !payload) {
return
}

Expand Down

0 comments on commit 0ef73c8

Please sign in to comment.