saidinesh5 / mongoose-cpp

A rewrite of https://github.com/Gregwar/mongoose-cpp
MIT License
10 stars 8 forks source link

multi-thread request process #6

Open HaoLiuHust opened 5 years ago

HaoLiuHust commented 5 years ago

if there are many connections to the port, the request is processed one by one, or there is a thread pool to process it?

saidinesh5 commented 5 years ago

@HaoLiuHust sorry for the delayed reply. Holiday time. So Happy Holidays!

At it's core we still use mongoose library, which is just a single threaded event loop. So requests are processed one by one. The purpose of this fork was to actually support offloading long running tasks like file upload/ to a threadpool, without breaking the mongoose library.

Take a look at https://github.com/saidinesh5/mongoose-cpp/blob/master/examples/examples.cpp#L34

bool hello_delayed(const std::shared_ptr<Request>& req, const std::shared_ptr<Response>& res)
{
            std::thread([=]
            {
                int duration = std::stoi(req->getVariable("duration", "3"));
                std::this_thread::sleep_for(std::chrono::seconds(duration));
                res->send("Hello after " + std::to_string(duration) + " seconds\n");
            }).detach();

            return true;
}

So if you think you will face a long running task, you will have to manually spawn a thread like this or use one of the nice ThreadPool libraries out there: https://github.com/progschj/ThreadPool