Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How does nodejs handle multiple request? #3151

Closed
Temepest74 opened this issue Dec 30, 2020 · 4 comments
Closed

How does nodejs handle multiple request? #3151

Temepest74 opened this issue Dec 30, 2020 · 4 comments

Comments

@Temepest74
Copy link

I read some documentations about how nodejs handle different requests from multiple users at once, but I cannot get my head around it without examples. So I have a question about the next code.

app.get('/oauth-callback', async (req, res) => 
{
  try {
    //get temporary auth code
    const responseBody = await axios.post('https://example.com/token');
    const userData = await axios.get(''https://example.com/token-2');

    const accessToken = responseBody.data.access_token;
    const refreshToken = responseBody.data.refresh_token;
    const expiresAt = responseBody.data.expires_in;

    //do smth with this data which may take some time
    });
    res.redirect('/'); //try to find where to redirect
  } catch (e) {
    console.error(e.message);
  }
})

Let's assume a user (A) and another user (B) access this endpoint at the same time. Is there a risk of user B changing the responseBody of user A? If so, when and why? Both requests to token and token-2 will give unique responses to each user.

@RaisinTen
Copy link
Contributor

I think it will be more helpful if you could share some code that runs with no errors. Also, responseBody is const + not modified in the code + it is not even shared with the callback called when a different user requests the server.

@Temepest74
Copy link
Author

I shared a small modified part of it due to NDA with my company.
Maybe rephrasing my question will help. Is there any risk of data (not a const one) being compromised inside an async function if too many requests at the same endpoint (in my case: oauth-callback) happen at the same time?

@RaisinTen
Copy link
Contributor

No, because the callbacks are executed sequentially. Here is a demonstration:
Here is an http server to log and increment a variable in an async callback for each request:

const http = require('http');

let data = 1;

http.createServer(async(req, res) => {
  console.log(data++);
  res.end();
}).listen(5000);

Here is a bash script to send 1000 requests to the server simultaneously:

#!/bin/bash

for i in {1..1000}
do
    curl localhost:5000&
done

When you run the two, you'll see the server log the numbers 1 to 1000. If anything were to go wrong with the data in the async http callbacks, data wouldn't be incremented as expected. That doesn't happen because these run in a single thread though the requests come in parallel.

@Temepest74
Copy link
Author

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants