libevent icon indicating copy to clipboard operation
libevent copied to clipboard

expose evhttp_serve to user

Open Coeur opened this issue 1 year ago • 4 comments

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.

Coeur avatar Jul 08 '24 09:07 Coeur

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)

zuiwuchang avatar Jul 08 '24 11:07 zuiwuchang

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.

azat avatar Jul 09 '24 05:07 azat

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.

zuiwuchang avatar Jul 10 '24 00:07 zuiwuchang

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).

zuiwuchang avatar Aug 09 '24 10:08 zuiwuchang

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.

theoden8 avatar Oct 21 '24 09:10 theoden8

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?

theoden8 avatar Oct 22 '24 18:10 theoden8

Also 91de0161d3fe0ff57db8a0bcd7589af6361058fe would fix #1578 if applied on top of exposing evhttp_get_request.

theoden8 avatar Oct 22 '24 18:10 theoden8

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 fd into i.e. bufferevent_pair this will simplify the queue
  • fix the copyright to something sensible

azat avatar Oct 27 '24 15:10 azat

@azat 986efdfe928f2b887236fce9eb49ce2e37b9adc3 @zuiwuchang mind if this rewritten code is included in libevent under BSD-3 clause?

theoden8 avatar Oct 28 '24 13:10 theoden8

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

azat avatar Nov 02 '24 19:11 azat

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.

Coeur avatar Nov 02 '24 19:11 Coeur

@azat 986efdf @zuiwuchang mind if this rewritten code is included in libevent under BSD-3 clause?

Nice to see this, i'll like.

zuiwuchang avatar Nov 04 '24 06:11 zuiwuchang