[erlang-questions] how to limit number of connections to gen_tcp?

Ulf Wiger ulf.wiger@REDACTED
Wed May 27 11:09:24 CEST 2009


Jarrod Roberson wrote:
> On Tue, May 26, 2009 at 1:48 PM, Ulf Wiger
> <ulf.wiger@REDACTED> wrote:
>> Jarrod Roberson wrote:
>>> I have the following code. How do I limit the number of connected clients.
>> I posted a sketch earlier this month:
>>
>> http://www.erlang.org/pipermail/erlang-questions/2009-May/043892.html
>>
>> BR,
>> Ulf W
> 
> thanks, but that is a little to abstract for me to understand how it
> applies as a solution to my problem.

Apologies. I was in a hurry. Given the additional requirements
you gave in a later mail (that you want to tell clients that
the server is overloaded), this model doesn't solve the problem.
Otherwise, a modification of my sketch could have been that the
acceptor process spawns a new acceptor when the whole job is
finished - not immediately after accept() returns.

In general, if the goal is to protect the server from overload,
it is better to leave the requests in the TCP buffers. This is
the cheapest and most effective way to guard against various
forms of attack. Once you've accepted the connection, the client
has the opportunity to start sending packets, further increasing
the load on your system.

Of course, in some cases, the protocol really requires you to
respond with some 'insufficient resources' reply. In order to
do that, you have to allocate server resources, and also
probably impose some penalty on all sessions in order to keep
track of when to accept the work and when to reject it.

You may also need to consider the case where the jobs crash
and don't report to the counter process that they've finished.
The process keeping track of the active sessions ought to have
a bullet-proof way of detecting the end of a session.

BR,
Ulf W
-- 
Ulf Wiger
CTO, Erlang Training & Consulting Ltd
http://www.erlang-consulting.com


More information about the erlang-questions mailing list