libmicrohttpd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libmicrohttpd] Idea for a good design to use external select + suspend/


From: silvioprog
Subject: [libmicrohttpd] Idea for a good design to use external select + suspend/resume + pthread
Date: Sun, 17 Mar 2019 02:28:24 -0300

Hello everyone.

I've tried to use an external select, the suspend/resume feature and a detached thread for some specific (slow) requests. The main idea around this, is: the common requests (database CRUDs, html/js/css sending etc.) are processed in the main loop within main thread (application), but, slow requests (large report generation, remote backups etc.) are suspended to be processed in a detached thread, which resumes the connection as soon as the request processing ends. However, I have had some difficulty to solve it because the MHD_queue_response() must be called by the main thread, not by detached thread(s).

After some googling I found two good related topics:

1. https://lists.gnu.org/archive/html/libmicrohttpd/2016-09/msg00000.html
2. https://lists.endsoftwarepatents.org/archive/html/libmicrohttpd/2016-10/msg00011.html

The 1. is the same problem I'm trying to solve, but I wouldn't like to use global variables. The 2. is exactly I'm looking for, but I couldn't find any code/example showing how the problem was solved, so I suspect he used the *con_cls instead of global variables to share some list reference to the detached thread(s).

After reading these topics I got two doubts. It is a good practice to use a shared vector (or any list + binary search) plus a mutex to manage each detached thread? If so, could the same list be used to be iterated to call the functions MHD_create_response_*() + MHD_queue_response() + MHD_resume_connection() to dispatch their respective responses? I need to take some care not to lose performance by choosing a bad design and, since some members tried to solve the same problem, I decided to ask.

I have a draft (in attachment) which I'll use to start/try the design using an external select + suspend/resume + pthread. It works fine if you uncomment the macro INTERNAL_SELECT to use the internal MHD loop or the macro TIMEOUT to use the external one. In this draft, the MHD_queue_response() is called in the detached thread, it is wrong, but it is just to understand how MHD works using external threads + suspend/resume. If you keep both INTERNAL_SELECT and TIMEOUT commented, the first request will never end unless another request arrives, I think a good design should solve it.

I would appreciate any good idea about this. If solved, it would be nice to convert it to a MHD example to share the solution for other members.

Thank you!

P.S.1: to test the draft above, use:

$ curl http://localhost:<PORT> # simulates a common request.
$ curl http://localhost:<PORT>/sleep # simulates a slow (about 10s) request.

P.S.2: I'm studying select()/epoll() and doing some exercises to fully understand them.

--
Silvio Clécio

Attachment: main.c
Description: Text Data


reply via email to

[Prev in Thread] Current Thread [Next in Thread]