Threads vs async events
Not strictly related to C programming, hence my post here. I'm currently writing a simple proxy server. Basically with my model so far, I spawn a thread per connection (there is a user-defined limit). However, I don't think that would scale too well. Plus most OSes I target only support ~100 threads per process.
At the moment all my proxy server does is read and forward data to another socket. So my question is, would I see an advantage switching to a library such as libevent? It should make thread issues disappear, and if I needed to work with the data I could always create a worker thread.
Just trying to get some feedback whether switching to an event driven loop with libevent is a better way to go.
If it matters, I'm targeting both Linux and Windows. Research shows MySQL has a connection to thread ratio of 1:1 (same as my program). However my requests would most likely take much longer to serve than MySQL requests.
I believe it's a good choice Zacs if indeed it fits your bill to change the concept to event-driven. However, you can achieve similar (or better) results by adopting not a thread-per-connection setting, but instead setting up a thread pool.
As far as I understand libevent, you will have a problems if later you do indeed want to add threads. libevent does not really have thread-safe dispatch and queuing. It's essential a single-threaded framework. They keep working on adding thread-safe features, but last time I checked it's still full of problems.
So you will be stuck to libevent. As far as scalability goes this may not be what you want. And you know that it will not be that easy to change your code from an event-based model back to a thread-based one, if you later regret the decision.
But if you feel that libevent will suit your scalability requirements, why not? It's a very good library (heck, it even supports buffered events! That's sweet.). Otherwise the norm for scalability still is Thread Pools.
EDIT: A thought occurred just now to me from this quote "At the moment all my proxy server does is read and forward data to another socket". If by any chance you need to preserve order of execution, you can indeed apply libevent to a thread pool, I think. I see no problem with that. You will need to pipe libevent to one thread. Essentially call event_init() in one thread and keep it that way. I think there may be other issues, but all will be solvable, because essentially what you will be doing is proxying the Thread Pool with libevent. Meanwhile you may also want to check libev
The apache solution to this is to use multiple processes with multiple threads.
That's through the MPM module and its in fact a pooling mechanism too. It's not a bad idea, but I doubt Zacs requirements demand such a killer.
Thanks for the input Mario, really helpful.
I think I will stick with threads, since it seems to work fine. And being a small, home or personal proxy the huge scalability isn't an issue as such. It wouldn't be too hard to switch to thread-pooling later if required too, if I stick with threads. That's good to know about apache, definitely in a different ball park.
Thanks again to both of you :).