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

Robert Raschke <>
Tue May 26 22:40:39 CEST 2009


On 5/26/09, Jarrod Roberson <> wrote:
> On Tue, May 26, 2009 at 1:20 PM, Alin Popa <> wrote:
>> Hi Jarrod,
>>
>> Something like this might help you:
>>
>> -define(MAX_TCP_CONNECTIONS, 1000).
>> -define(TCP_OPTIONS, [binary, {packet, 0}, {active, false}, {reuseaddr,
>> true}, {backlog, ?MAX_TCP_CONNECTIONS}]).
>>
>> listen(Port, F) ->
>>      {ok, LSocket} = gen_tcp:listen(Port, ?TCP_OPTIONS),
>>      accept(LSocket, F).
>>
>> .............
>>
>> Hope this is what you are looking for ...
>
> This will do what I asked about but it doesn't do what I was
> "thinking' about :-)
>
> What I really want to be able to do is accept the connection and if
> max connections is exceeded send a message to the client that the
> server is overloaded and the close the connection. What I really can't
> figure out is how to pass around  a "current connection count" to I
> can increment it and decrement it during accepts and disconnects.

I think Mochiweb has something like this. It doesn't send a special
return message, but uses it's own counter to manage the allowed max
connections. This is from memory, I don't have the code in front of
me, so I may not be correct. Have a look in the code that implements
the socket server.

Robby



More information about the erlang-questions mailing list