Skip to content

Commit 4837e61

Browse files
authored
Merge pull request #28 from fabik111/master
Change updateToken retry policy
2 parents 35cb422 + 984c8a8 commit 4837e61

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

utils/arduino-connection-manager.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const Mutex = require('async-mutex').Mutex;
3737
*/
3838
var connections = [];
3939
const getClientMutex = new Mutex();
40-
40+
var numRetry=0;
4141

4242

4343
async function getToken(connectionConfig) {
@@ -239,13 +239,26 @@ async function updateToken(connectionConfig) {
239239
if (user !== -1) {
240240
var tokenInfo = await getToken(connectionConfig);
241241
if (tokenInfo !== undefined) {
242+
numRetry=0;
242243
connections[user].token = tokenInfo.token;
243244
connections[user].expires_token_ts = tokenInfo.expires_in;
244-
connections[user].clientMqtt.updateToken(tokenInfo.token);
245-
connections[user].clientHttp.updateToken(tokenInfo.token);
245+
if(connections[user].clientMqtt){
246+
connections[user].clientMqtt.updateToken(tokenInfo.token);
247+
}
248+
if(connections[user].clientHttp){
249+
connections[user].clientHttp.updateToken(tokenInfo.token);
250+
}
246251
connections[user].timeoutUpdateToken = setTimeout(() => { updateToken(connectionConfig) }, tokenInfo.expires_in * 1000);
247252
} else {
248-
connections[user].timeoutUpdateToken = setTimeout(() => { updateToken(connectionConfig) }, 1000);
253+
/*Avoid too many requests addressed to server*/
254+
if(numRetry < 3){
255+
connections[user].timeoutUpdateToken = setTimeout(() => { updateToken(connectionConfig) }, 5000);
256+
}
257+
else{
258+
connections[user].timeoutUpdateToken = setTimeout(() => { updateToken(connectionConfig) }, 60000);
259+
}
260+
261+
numRetry++;
249262
}
250263
}
251264
} catch (err) {

0 commit comments

Comments
 (0)