使用线程池的多线程服务器
我正在规划一个多线程服务器(准确地说是 SCGI)。现在,我知道每个连接使用一个线程的传统方法可扩展性不太好。我也不想使用像 libevent 这样奇特的东西,因为这是一个业余爱好项目,我不希望在我的代码库中有很多依赖项。
我想到的方法是使用线程池并让一个线程侦听网络以对传入的任何请求进行排队。然后由池管理的线程将请求出队,接收数据并分别进行响应。
这样,我就不会产生不断创建线程的开销,同时仍然能够并行处理许多请求。
这个架构设计中是否存在一些我不知道的基本问题,或者这是一个不理想但仍然可以的解决方案?
谢谢!
I'm planing a multithreaded server (SCGI to be precise). Now, I know that the traditional approach using one thread per connection is not very scalable. I also don't want to use something fancy like libevent, as this is a hobby project and I prefer not to have lots of dependencies in my codebase.
The approach I'm thinking of is to use a threadpool and let one thread listen to the network to queue up any request coming in. The threads managed by the pool then dequeue the requests, receive the data and respond respectively.
This way, I wouldn't have the overhead of constant thread creation while still being able to serve many request in parallel.
Is there some fundamental problem in this architecture design that I'm not aware of or is this a not-ideal-but-still-ok solution?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来不错。根据该服务器预计会看到的负载量,这可能是一个矫枉过正的情况。
每个连接一个线程就足够了,直到您开始并行处理数十个甚至数百个请求。每个连接一个线程的优点是简单,并且可能不值得放弃这一点。
另一方面,如果您正在寻找需要处理大量流量的东西(无论是像 webproxy 这样的外部流量还是像 memcache 这样的内部流量),您可能应该只使用 libevent。 AFAIK 所有的大佬都在使用它或者非常类似的东西(memcache、haproxy 等等)
最后,如果你这样做只是为了好玩,那么就使用你想要的任何东西:) 使用所有这些架构可以获得良好的性能。
Looks good. Depending on how much load this server is expected to see this might be an overkill.
One thread per connection is good enough until you start handling dozens if not hundreds requests in parallel. The advantage of one thread per connection is the simplicity, and it might not be worthwhile to give that up.
On the other hand if you are looking for something that needs to handle tons of traffic (either external like webproxy or internal like memcache) you probably should just use libevent. AFAIK all the big boys are using it or something very similar (memcache, haproxy and so on)
Finally, if you are doing this just for fun just use whatever you want :) It's possible to get good performance with all those archs.