2

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:

  1. is setInterval performance wise good to be used in production (many users)?
  2. 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.
user938363
  • 9,990
  • 38
  • 137
  • 303

2 Answers2

1

You can use cron-job instead of setInterval. Here is link of node-cron https://www.npmjs.com/package/node-cron.

Instead of using global variable, you can use local storage and update that. You can access data from it anywhere in your application.

  • User is request the access key via HTTP Get. Is local storage as fast as global variable? If global variable is in memory, then it will be faster than local storage. – user938363 Aug 01 '20 at 13:47
1

In your case, I recommend to use cron job.

Advantages of cron solution

  • less memory consumption for 90% of the time
  • memory leaks are effectively eliminted
  • the code is reloading all libraries every time so updates take effect on the next run without additional complexity

Advantages of daemon solution

  • load time only happens once so disk I/O and CPU are lower for subsequent runs since you won't be parsing your source code or pulling in all of the libraries
  • the lower CPU and disk I/O for subsequent runs also means that there is more of those resources available for whatever your main application for the Raspberry may be.
  • Linux should swap out the memory you're not using. This could be really slow depending on your storage though.

https://serverfault.com/questions/853930/cron-job-vs-nodejs-setinterval-for-optimal-performance/854096#854096

And instead of setting the variable to the global variable, you can save it to a dedicated table in a database.

So your cron job saves your key in a table and your api will load the key from the database.

critrange
  • 5,652
  • 2
  • 16
  • 47
  • I thought global variable is in memory and shall provide fast access. The access key is temporary data and is only less than 1kb. There is no need to keep it persistent. – user938363 Aug 01 '20 at 13:53
  • if you restarted the app, the global variable is not available until you set it again – critrange Aug 01 '20 at 13:55