Closed PedroKantar closed 6 years ago
Maybe it would be better to do it with an increasing timeout depending on the number of retries with a maximum value to avoid a too large timeout :
const retryTimeout = Math.min(retries * 1000, 30 *1000);
With this solution, we can reconnect instantly if the disconnection was just a temporary network failure instead of waiting for 15 seconds.
Feel free to submit a PR, if you have time to implement it !
In V1.1.4 the retryCount is never reseted :
The line retryCount = 0; // Reset retry count on successful
in the socket.connect()
connection has no effect, since the retryCount
value will be taken from the last bind retry function :
// Retry on disconnect
retry = connect.bind(
this,
payload,
RequestType,
preBuffer,
NotificationSchema,
keys,
persistentIds,
retryCount + 1
);
I tried to find a "nice" solution based on current code (I would have submitted a PR), but I ended up with the solution I proposed in my first commit, here after:
[FIX] High CPU consumption when internet connection is lost #2, where it was based on defining "let retryCount = 0;
" outside "async function connect()
" scope..
🤔
I'm going to submit a PR..
Matthieu,
When the internet connection is lost, in client/socket/index.js ,
connectSocket()
starts looping onretry()
, which results in a nasty CPU overhead.To prevent it, I modified
connectSocket()
prototype, by adding a "retry tempo" parameter, so that updated code in index.js looks like that:const RETRY_TEMPO = 15*1000;
connectSocket(retry);
becomes:connectSocket(retry, retryTempo);
retry();
becomes:setTimeout(function(){ retry(); }, retryTempo);
const socket = await connectSocket(retry);
becomes:const socket = await connectSocket(retry, RETRY_TEMPO);
What do you think? I tested it and it works quite well. I set the tempo to 15sec, even if it could be set at interface level by adding a new parameter to
function connect()
.Regards, Pedro.