Blocking request while route takes time in single client #1384
-
Hi to all, First of all i want to say tanks for this awesome library, There is a situation that it made me stuck in usage of threaded environment, in real world I have an api that it takes time to respond, do many calculation and database operations, this API occasionally calls by user, but when this api calls by user after just only few requests, server will lock all other requests till the heavy API finish. #include "App.h"
#include <thread>
#include <algorithm>
#include <mutex>
/* Note that SSL is disabled unless you build with WITH_OPENSSL=1 */
const int SSL = 1;
std::mutex stdoutMutex;
int main()
{
/* Overly simple hello world app, using multiple threads */
std::vector<std::thread *> threads(/*std::thread::hardware_concurrency()*/ 3);
std::transform(threads.begin(), threads.end(), threads.begin(), [](std::thread * /*t*/) {
return new std::thread([]() {
uWS::SSLApp({.key_file_name = "your.key", .cert_file_name = "/your.crt"})
.post("/delayedAPI",
[](auto *res, auto * /*req*/) {
std::this_thread::sleep_for(std::chrono::milliseconds(60000));
res->end("Delayed Hello world!");
})
.post("/fastAPI",
[](auto *res, auto * /*req*/) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
res->end("Fast Hello world!");
})
.listen(5050,
[](auto *listen_socket) {
stdoutMutex.lock();
if (listen_socket) {
/* Note that us_listen_socket_t is castable to us_socket_t */
std::cout << "Thread " << std::this_thread::get_id() << " listening on port "
<< us_socket_local_port(SSL, (struct us_socket_t *)listen_socket) << std::endl;
} else {
std::cout << "Thread " << std::this_thread::get_id() << " failed to listen on port 3000"
<< std::endl;
}
stdoutMutex.unlock();
})
.run();
});
});
std::for_each(threads.begin(), threads.end(), [](std::thread *t) { t->join(); });
}
Just compile and run the executable. As you see there are only two route ( any suggestions? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
You're not allowed to block in a handler. You can look at the copious amounts of tutorials for node.js |
Beta Was this translation helpful? Give feedback.
-
This tells me you're not actually interested in listening but just want to continue down your path either way. Everything you ask for has been thoroughly explained in detail by a thousand men already and there are literally mountains of material on this. Stop what you're doing, get the basics right, then reconsider |
Beta Was this translation helpful? Give feedback.
-
Real men don't read the manual ;P |
Beta Was this translation helpful? Give feedback.
You're not allowed to block in a handler. You can look at the copious amounts of tutorials for node.js