Efficiently accepting TCP connections

klacke <>
Thu May 12 22:59:08 CEST 2005


On Thu, May 12, 2005 at 02:11:55PM +0300, joel reymont wrote:
> I promise I'll go and read through all the gen_tcp threads as soon as
> I'm done sending this message but...
> 
> Is there an efficient idiom for handling gen_tcp connections on the server side?
> 



I increased the performance of yaws sufficently much so  
that I went ahead with the following scheme:

I spawn the acceptor sockets, but when an acceptor
socket is done with it's job. instead of terminating, it
goes into idle loop and await an new job.


This pretty much just saves the process creation overhead 
but increased performance. 


Otherwise the normal best practice is to loop
with the listen socket like this:


listenloop(LS) ->
   S = self(),	       
   spawn(fun() ->
       A = accept(LS),
       S ! next,
       handle_session(A)
   end),
   receive next -> ok end,
   listenloop(LS).


But as I said, keeping the acceptor process around 
for some time can increase performance.


Another thing that majorly can increase performance is to
utilize the socketoptions [{packet, X}] in such a way
so that all buffering is done in the inet_drv.c and you
can be certain in your erlang code that you get "full 
messages"


/klacke




-- 
Claes Wikstrom                        -- Caps lock is nowhere and
http://www.hyber.org                  -- everything is under control          LsockLsock)[



More information about the erlang-questions mailing list