简体   繁体   中英

is setInterval in post request bad for server performance - expressjs

I set a interval to poll a user's Spotify account for playback info when they make a post request to my express server.

app.post('/party', _ensureAuthenticated, function(request, response) {
  const token = decrypt(request.user.encryptedAccessToken);
  setInterval( () => {
    pollSpotify(token)
  }, pollInterval);
});

My question is, if multiple users make this post request for different accounts, setting many intervals on express server, will this degrade my servers performance? How can I clear these intervals overtime? What is a better way to poll spotify from my express server?

will this degrade my servers performance?

Yes, building up more and more setInterval() timers will eventually affect your server performance. How much it affects the server depends upon what your pollInterval is and how much processing each interval has to do and how many timers you have.

And, since you provide no ability to EVER stop one of these timers, they will pile up forever, even days after a user has left your web site. And, you'd even get duplicates for a given user is they go to /party more than once.

To suggest a better design, I'd have to understand what you're doing in pollSpotify(token) and what you're doing with the results you find there.

At a minimum, you need the ability to know when a user is no longer on your site so you can stop the timer that's running for them and prevent duplicates for a given user.

It would be even more scalable if you just had one polling timer that runs all the polling and it checks on behalf of each of the items in array of users/tokens that are active. You could then just add/remove items from the array as users request it or timeout as inactive.

And, of course you need to know when users are no longer active on your site so you can stop polling on their behalf.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM