Skip to content

Commit

Permalink
🐛 Reset retry count on socket successful connection, Fix #2
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroKantar authored and MatthieuLemoine committed Nov 20, 2017
1 parent f89bde2 commit e5264cd
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions src/client/socket/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const decrypt = require('../../utils/decrypt');
const HOST = 'mtalk.google.com';
const PORT = 5228;
const RETRY_MAX_TEMPO = 15; // maximum time before retrying to open the socket (in seconds)
let retryCount = 0; // retries count of opening socket attempts

const ON_NOTIFICATION_RECEIVED = 'ON_NOTIFICATION_RECEIVED';

Expand All @@ -23,8 +24,7 @@ async function connect(
preBuffer,
NotificationSchema,
keys,
persistentIds,
retryCount = 0
persistentIds
) {
// Retry on disconnect
const retry = connect.bind(
Expand All @@ -34,10 +34,9 @@ async function connect(
preBuffer,
NotificationSchema,
keys,
persistentIds,
retryCount + 1
persistentIds
);
const socket = await connectSocket(retry, retryCount);
const socket = await connectSocket(retry);
timer = process.hrtime();
console.info('MCS client connected');
// Payload to send to login with MCS server
Expand All @@ -49,14 +48,14 @@ async function connect(
return listen(socket, NotificationSchema, keys, persistentIds);
}

function connectSocket(retry, retryCount) {
function connectSocket(retry) {
return new Promise(resolve => {
const socket = new tls.TLSSocket();
socket.setKeepAlive(true);
socket.on('close', () => {
const diff = process.hrtime(timer);
const minutes = Math.round((diff[0] + diff[1] / 1e9) / 60);
const retryTempo = Math.min(retryCount, RETRY_MAX_TEMPO);
const retryTempo = Math.min(retryCount++, RETRY_MAX_TEMPO);
console.warn(`MCS socket closed after ${minutes} minutes`);
console.warn(`Will try to reconnect in ${retryTempo} seconds`);
setTimeout(() => retry(), retryTempo * 1000);
Expand Down

0 comments on commit e5264cd

Please sign in to comment.