What's the best way to handle incoming messages?

泪湿孤枕 提交于 2019-12-10 12:53:35

问题


I'm writing a server for an online game, that should be able to handle 1,000-2,000 clients in the end. The 3 ways I found to do this were basically:

  1. 1 thread/connection (blocking)
  2. Making a list of clients, and loop through them (non-blocking)
  3. Select (Basically a blocking statement for all clients at once with optional timeout?)

In the past I've used 1, but as we all know, it doesn't scale well. 2 is okay, but I have mixed feelings, about one client technically being able to make everyone else freeze. 3 sounds interesting (a bit better than 2), but I've heard it's not suitable for too many connections. So, what would be the best way to do it (in D)? Are there other options?


回答1:


The usual approach is closest to 3: asynchronous programming with a higher-performance select alternative, such as the poll or epoll system calls on Linux, IOCP on Windows, or higher-level libraries wrapping them. D does not support them directly, but you can find D bindings or 3rd-party D libraries (e.g. Tango) providing support for them.

Higher-performance servers (e.g. nginx) use one thread/process per CPU core, and use asynchronous event handling within that thread/process.




回答2:


One option to consider is to have a single thread that runs the select/pole/epoll but not process the results. Rather it queues up connections known to have results and lets a thread pool feed from that. If checking that a full request has been read in is cheap, you might do that in the poll thread with non-blocking IO and only queue up full requests.

I don't know if D provides any support for any of that aside from (possibly) the inter-thread communication and queuing.



来源:https://stackoverflow.com/questions/9025986/whats-the-best-way-to-handle-incoming-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!