Re: Socket Programming

From: David Steuber (david_at_david-steuber.com)
Date: 05/27/04


Date: 27 May 2004 12:22:24 -0400

Eric Marsden <emarsden@laas.fr> writes:

> [reacting a bit late to this interesting thread]

I've actually been thinking about this recently. Inside the last hour
in fact. Just to recap, there are two basic models I would consider
using for a TCP service:

* Single threaded application that multiplexes concurrent connections
  and uses non-blocking io.
* Multi threaded application that uses one thread per connection
  request and blocking io.

Some requests may be served within millisecond time scale. Others may
take a few seconds. Hopefully no requests take longer than that
because people on the other end of the line get impatient for an
answer very quickly.

If the host machine is a multi-cpu box and you have native threads,
then you can take advantage of the hardware with the second approach.
On a single cpu system, preemptive thread switching introduces some
overhead. In either case, I would expect the application logic to
have similar complexity. However, I personally am more familiar with
mult-threaded programming techniques from other languages than I am
with the multiplexing approach (select in Perl & C).

I would tend to favor the multi-threaded approach for that reason even
on a single cpu system (in the hope that I can upgrade to a multi cpu
system in the future). Is there a noticable performance downside to
this in terms of maximum requests per second I can handle on a given
piece of hardware?

Just to keep this on topic, I would be using SBCL. It only supports
native threads on x86 at present. I haven't measured it's thread
creation overhead yet, but I plan to Real Soon Now(tm).

One thing that would be nice to do with a multi threaded approach is
in the main thread that accepts socket connections and spawns new
threads to handle them is have a mechanism for sleeping for some
fractions of a second to act as a throttle in the event of the
slashdot effect or a plain old DoS attack.

In C, sleep takes an int in seconds of time. The best I can think to
do is keep some rolling counter of number of connections accepted in
the past second and sleep for a second or two if the number exceeds
some threshold value. The counter would have to be checked and
adjusted with each accept.

A DoS attack would still work, but the system won't fall down.

-- 
An ideal world is left as an excercise to the reader.
   --- Paul Graham, On Lisp 8.1


Relevant Pages

  • Re: IOCP Across Threads - Error 995
    ... The problem is that when a thread exits with outstanding overlapped ... send requests in progress all of those requests are cancelled - See: ... I have created an additional thread that initiates connections to the ... I placed a Sleepat the end of the outgoing connection ...
    (microsoft.public.win32.programmer.networks)
  • Re: Network Firewall/Routing Solution
    ... >> firewall combo boxes that linksys sells, and I really don't want to run ... >> not working properly at all with multiple network cards. ... >> like Unicode and header information for http requests, ... >> non-pasv connections. ...
    (comp.security.firewalls)
  • zombies with AMD64 and 32 bit userspace with 2.6
    ... the test is a simple forking proxy that receives connections from one ... machine (running apache benchmark) and forwards them to anothr machine ... to do a test with more then about 30,000 requests in it the box starts to ... the smaller tests leave no zombies at all and can be run multiple times ...
    (Linux-Kernel)
  • Network Firewall/Routing Solution
    ... for a good solution to route inbound and outbound traffic. ... not working properly at all with multiple network cards. ... I will need to deal with inbound web and ftp requests from the internet ... able to filter the connections that are established to these IPs for things ...
    (comp.security.firewalls)
  • No buffer space available?
    ... connection problems under heavy loads-- HTTP requests don't return, ... connections to the MySQL server fail, ... requests for memory delayed ...
    (freebsd-questions)