Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to handle incoming messages?

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?

like image 754
Mars Avatar asked Jan 26 '12 21:01

Mars


People also ask

What is an incoming message?

Incoming messages are displayed as notifications that animate down from the top of the screen. Message notifications should be displayed if: They are important enough to interrupt the driver. They are appropriate for driving.


2 Answers

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.

like image 173
Vladimir Panteleev Avatar answered Sep 29 '22 06:09

Vladimir Panteleev


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.

like image 35
BCS Avatar answered Sep 29 '22 05:09

BCS