My nodejs (12.18) project needs to retrieve an access key from remote server and pass it to users who request it. The access key would be updated every 5 minutes. a setInterval
is used to retrieve access key every 5 minutes from remote server and the access key is stored in a global.variable
for late read access.
const helper = require("../lib/helper");
global._cloudStorageAccessInfoObject = {}; //<<==declare global variable
var bucket_name, roleSessionName, policy, accessObj;
policy = JSON.stringify({
//my policy
});
bucket_name = "xxx-cloud-1";
roleSessionName = 'myapp';
setInterval(async () => {
accessObj = await helper.getOSSstsToken(bucket_name, roleSessionName, policy);
if (accessObj && accessObj !== {} && accessObj.accessKeyId) {
_cloudStorageAccessInfoObject = accessObj; //<<==access key assigned to global variable
}
}, 1000*60*5); //<<==retrieve access key once every 5 minutes
The code above works fine so far. There are 2 questions about it:
- is
setInterval
performance wise good to be used in production (many users)? - There are post talking about
bad global variable
. Is there other better alternative solution to store access key for late use in the app. Currently the code above is the only code updating the global for read access late on in the app.