3 ms·
Generally you make sure your network code is separate from any blocking activity. For example, make a light "network plugin" that deals with your core function
by axod 18y ago
Generally you make sure your network code is separate from any blocking activity.
For example, make a light "network plugin" that deals with your core functionality, then if it needs to go to DB, or do some potentially blocking action, have it call a "helper" which has it's own thread.
For instance, Mibbit handles around 7,000 connections at the peak of the day (3.5k in, 3.5k out). Those are all handled in a single thread. There's an additional thread that handles dns lookups, one to do database stuff. Each one of those just has a synchronized queue listing the jobs, each with a callback to update the network plugin with a reply.
The dns could actually be done asynchronously easily enough, but the gain wouldn't be worth it right now.
It depends on how lengthly other processing is going to be. Quite often you can separate the work up into chunks and just do a little each time (Kind of like your own time slicing). Or split it out into its own dedicated thread and have a job queue as above.
- davidw 18y ago> Quite often you can separate the work up into chunks and just do a little each time (Kind of like your own time slicing). I was going to mention this above, but this is one of the things that makes Erlang cool - it does that automatically, so that you just write your code, and it takes care of the scheduling.