简体   繁体   English

线程,事件循环以及大量的连接和并发

[英]Threading, event loops and large amounts of connections and concurrency

all. 所有。 I'm trying to figure out the best method of handling large amounts of concurrent connections (upwards of more than 800k) in the best manner possible. 我试图找出以可能的最佳方式处理大量并发连接(超过800k)的最佳方法。 I've decided to go with libevent for handling reads/writes on the sockets, and will be using one port on the back end and handing the fd around on non-blocking sockets. 我已经决定使用libevent处理套接字上的读/写,并且将使用后端的一个端口并将fd传递给非阻塞套接字。 Where my issue(s) come in to play: 我的问题在哪里出现:

1) libevent with regards to event bases - If I were to, for instances, run one thread per a core each with its own libevent base listening on a single fd for incoming connections, how would libevent handle multiple event bases being triggered on that single fd? 1)关于事件基础的libevent-例如,如果我要在每个内核上运行一个线程,每个线程都有自己的libevent基础,并在单个fd上侦听传入的连接,那么libevent将如何处理在该基础上触发的多个事件基础FD? The idea would be to then take that incoming connection, accept it and start a new event base on the new fd specific to that one connection. 想法是然后采用该传入连接,接受该连接,并基于特定于该连接的新fd启动一个新事件。 Or, is the appropriate way to do it as I have in the past - run the primary event base in the main program thread and push the incoming connection off to a worker thread to handle the accept, and then create a new event base for each connection then? 或者,像过去一样,这是合适的方法-在主程序线程中运行主要事件库,并将传入的连接推入工作线程以处理接受,然后为每个事件创建新的事件库连接呢?

2) Threads per a connection... Yes, or no? 2)每个连接的线程数...是或否? In past implementations, I've done a model of 1:1 threads per an accepted connection. 在过去的实现中,我已经为每个可接受的连接建立了1:1线程的模型。 The end result is obviously with 500 clients connected, 500 threads plus whatever threads I've used as a work queue. 显然,最终结果是连接了500个客户端,500个线程以及我用作工作队列的任何线程。 However, I'm concerned that this may pose an issue once I reach in to the hundreds of thousands marker connection-wise... Can anyone confirm? 但是,我担心一旦接触到成千上万个标记连接,这可能会带来问题。有人可以确认吗? I also feel like having that many threads when using something with asynchronous IO such as libevent is not necessary and simply adds more overhead than what's needed... But I could be wrong. 我还觉得在使用带有异步IO的东西(例如libevent)时不需要那么多线程,而只是增加了比所需更多的开销……但是我可能错了。

This is the first time I'm having to write something that will support this high of a user load, and I'd prefer to be able to write it from scratch from a solid conceptual design as a means of fully understanding everything at play. 这是我第一次必须编写能够支持如此高的用户负载的东西,并且我更希望能够从扎实的概念设计中从头开始编写它,以完全理解游戏中的所有内容。 Obviously I could go dig through something like UnrealIRCD or nginx's source code and come up with a solution, but I'd really prefer to do it from an understanding of what it is I'm writing and why I'm writing it that way. 显然,我可以深入研究UnrealIRCD或nginx的源代码,并提出解决方案,但我真的更喜欢这样做,因为我了解我在写什么以及为什么要这样写。 So some feedback would be greatly appreciated. 因此,一些反馈将不胜感激。

I believe you may be confusing two different programming paradigms. 我相信您可能会混淆两种不同的编程范例。 If you are using libevent, each thread will handle hundreds or thousands of connections. 如果使用的是libevent,则每个线程将处理数百或数千个连接。 For more information as to where to begin developing a high volume/concurrent server search for the C10K problem. 有关从何处开始开发高容量/并行服务器的更多信息,请搜索C10K问题。 Here is a page to start: 这是开始的页面:

http://www.kegel.com/c10k.html http://www.kegel.com/c10k.html

BTW, if you want to scale to 800K concurrent connections you should not use a "thread per connection" model. 顺便说一句,如果要扩展到800K并发连接,则不应使用“每个连接线程”模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM