Client/server message passing system

Status
Not open for further replies.

russ-iha

Ars Praefectus
4,407
Subscriptor
Just soliciting ideas on a client/server messaging system. Ideas, suggestions, etc.


Currently, I am thinking along these lines:

SERVER:

+ Main thread listens on a certain port. When a request comes in on that port, it spawns a WORKER to handle the request and goes back to listening. My thoughts on this: requests need to be grabbed quickly so the port isn't tied up. I know on a massive system this might cause lots of thread creates/deletes, but I rarely expect more than 1000 destinations, and maybe 5% are sending at once (high estimate IMHO), so that's 50 threads at most. I figure average rate is gonna be closer to 10 threads at most.

+ WORKER receives the data. Normally it is a send message request. (for heartbeat req see below) First step is to see if recipient is listed in the ACTIVE table; if so, it tries to deliver to the destination. If it succeeds, then all is well. If it fails, it drops the message into WAITING and also removes the destination from the ACTIVE table. If the destination is not listed in ACTIVE, it just immediately drops it into WAITING.


+ GARBAGE. Clients must send heartbeat every 15 minutes. Last heartbeat is entered into ACTIVE table. GARBAGE runs every 20 minutes, and any clients that have not sent heartbeat since 20 minutes are removed from the ACTIVE table. GARBAGE also ticks all messages in WAITING table; if a message receives more than X ticks (maybe 100,000?) it is considered orphaned and removed.


+ WORKER (heartbeat). If the data is a heartbeat signal, then worker just updates the last heartbeat field in ACTIVE and sends an OK response to the client.


+ WORKER (connect). When client connects, worker authenticates with whatever auth package admin has set, enters client into ACTIVE, then retrieves all messages for client that are in WAITING. Auth for starters it will be a USERS table, but i plan on having AD integration at some point


+ WORKER (disconnect). When client shuts down orderly, it sends disconnect. Worker removes client from ACTIVE table.

In this configuration, ACTIVE should have a relatively up-to-date picture of what destinations are currently reachable. WAITING stores all messages for clients that are not, to be delivered upon next connect.

Garbage ensures that you don't end up with undelivered messages in WAITING from 10 years ago causing the database to grow forever, and also ensures that if a destination goes offline suddenly without sending disconnect, it won't be stuck listed as 'online' forever.

I haven't quite worked out the best way to do the client yet... but it will probably be something like this:

This single-threaded app handles the UI and stuff. It also listens on the proper port. Whenever a server has a message for this destination, it will connect to this port to deliver it. The IP/port combo is listed in Active when client connects.

Received messages are posted where appropriate, and any messages to be sent are handled as needed.


Opinions? I think a setup like this would scale fairly well to a few thousand users at most, would allow for timely (yet disconnected) delivery of messages, and so forth.
It also has the added benefit of being simple to implement.
 
D

Deleted member 5324

Guest
If you want this to be scalable to or past a few thousand users, you almost certainly won't be able to use a thread for each user. The creation/destruction costs will be expensive, but even more costly will be the context switching. You'll want to go with some sort of asynchronous solution (which a proper asynchronous networking library should make rather easy to code.)

On the client side, remember that sockets are two-way. The server can just send messages over the socket on which the client is connected to it. Opening a port on the client side opens up a world of security issues in addition to making operation behind a firewall almost impossible.

To slightly improve scalability of the protocol, you might want to try instead of expecting a heartbeat from the client every X minutes, make the protocol such that the server can send an "echo request" that the client must respond to or be disconnected. This will let you (on the server side) shorten the amount of time zombied clients stick around if you need to.

100,000 20 minute increments will be 333,333 hours. With 168 hours in a week, that would be more than 6 years. You might want to decrease that :)

Jeremy
 

russ-iha

Ars Praefectus
4,407
Subscriptor
OK then... the problem is if the client opens a socket to the server, then I have to figure out some way to manage all that.

In that scenario, 1000 clients connected. How am I managing these clients dumping messages back and forth? Sure I can setup async and just get an event when data arrives, but I have to have some way of figuring out who sent the data... plus won't that serialize things? If I have only one event handler for data received.


The target platform is .NET, so I would be using the TCPListener and TCPClient classes.


Im off to do some research and think about this ;)
 

russ-iha

Ars Praefectus
4,407
Subscriptor
OK I suppose I could do something along the lines of a thread pool manually, but that's quite a bit of work for just starting out on this project.


Basically, I'd have to have a ThreadManager thread, which would spawn x number of WORKER threads.

Whenever main got data from a client, it would wrap it in a TASK object and hand that to the ThreadManager... TM would see if any threads aren't busy, and if so hand the TASK to a thread to complete it. If all threads are busy, it would shelve the task until a WORKER posted a TaskComplete message.


Anyone aware if .NET has a threadpooling mechanism built-in by chance? That would make things so much easier ;)

[edit]

Looks like .NET already includes a thread manager... this could work rather well actually.
 
Status
Not open for further replies.