expose evhttp_serve to user
Fixes #1471 ALTERNATIVE SOLUTION OF #1683 and #1684. Cherry-pick of #1685 (only the first two commits).
This PR is aimed at moving forward with the remaining tasks for the 2.2 milestone: https://github.com/libevent/libevent/milestone/4.
This PR is based on @zuiwuchang pull request and exposes evhttp_serve instead of evhttp_get_request.
I did not write a test for it and do not plan to write one myself: I'd like instead those interested in that feature to share a snippet of code that would demonstrate its usage (@zuiwuchang, @azat, ...), and then we could adapt it as a test.
I'm currently developing on a defective chip, and the asynchronous listener on this platform doesn't work properly, so I can only use a synchronous listener in a separate thread to accept connections and then use evhttp_serve to provide http services.(This is the problem I really need to solve right now https://github.com/powerpuffpenguin/ejs).
In addition, a useful scenario I can envision is to make full use of multi-core CPUs, so create multiple bases and asynchronous listeners, and then use some kind of load balancing algorithm to call evhttp_serve so that http processing can be relatively evenly distributed to different CPUs for processing.
I'll try to take the time to write demo code for the two scenarios above, but I currently have a lot of other code to complete and can't guarantee when the demo will be released. (So it would be great if someone else could post demo code, but if no one keeps posting I'll try to find the time to do this)
In addition, a useful scenario I can envision is to make full use of multi-core CPUs, so create multiple bases and asynchronous listeners, and then use some kind of load balancing algorithm to call evhttp_serve so that http processing can be relatively evenly distributed to different CPUs for processing.
Note, that for load balancing you can use SO_REUSEPORT
I'll try to take the time to write demo code for the two scenarios above
Thanks, looking forward for this. Don't get me wrong, this new API is simple, but I want to ensure that it will have users, and at least minimal coverage.
Note, that for load balancing you can use SO_REUSEPORT
SO_REUSEPORT is not supported by some systems such as windows. In addition, not using SO_REUSEPORT but doing load balancing yourself can more flexibly control the load balancing algorithm used.
I wrote a demo code for evhttp_serve in my free time, I hope it will be useful.
https://github.com/zuiwuchang/libevent_http_serve
I think evhttp_serve is a very useful function, especially when creating some underlying frameworks (such as https://github.com/powerpuffpenguin/ejs). So I hope someone can improve this function, thank you.
Although I would also like to complete it myself, I am currently developing some other code and do not have enough time to do this. Secondly, I am not actually a C programmer, so I don’t know much about how to make C code compatible with multiple operating system platforms (There are also other details of C that I am not very good at. For example, I don’t quite understand how to perform unit testing elegantly in C).
We use it here also https://github.com/clostra/newnode/blob/1dcdbbc7e8de78e041526a10449367b21fd00398/http.c#L233 Looking forward to this PR. I don't think we can use libutp setup as a test case as that would involve external dependencies.
I compressed @zuiwuchang 's example into one file:
https://gist.github.com/theoden8/5135d804c51b2d9da6a3556bc63c70f7
It's about 400LOC. Not all of them are necessarily what this (or the alternative) pull request needs. Any suggestions?
Also 91de0161d3fe0ff57db8a0bcd7589af6361058fe would fix #1578 if applied on top of exposing evhttp_get_request.
It's about 400LOC. Not all of them are necessarily what this (or the alternative) pull request needs. Any suggestions?
Yes, this will be a good example, we can put into the sample folder, this will be enough.
Couple of comments on it:
- we can get rid of unnecessary code (i.e. either parsing of address should be added or removed completelly)
- simplify error paths with
goto - use
[]for indexing in array - we can write the
fdinto i.e.bufferevent_pairthis will simplify the queue - fix the copyright to something sensible
@azat 986efdfe928f2b887236fce9eb49ce2e37b9adc3 @zuiwuchang mind if this rewritten code is included in libevent under BSD-3 clause?
Also https://github.com/libevent/libevent/commit/91de0161d3fe0ff57db8a0bcd7589af6361058fe would fix https://github.com/libevent/libevent/issues/1578 if applied on top of exposing evhttp_get_request.
@theoden8 let's submit fix on top later, separately
Those interested in the feature should open their own PR. I wrote regarding adding a test for this feature:
do not plan to write one myself
I'm glad @zuiwuchang and @theoden8 are interested, so please offer a pull request.
@azat 986efdf @zuiwuchang mind if this rewritten code is included in libevent under BSD-3 clause?
Nice to see this, i'll like.