gen_server, gen_tcp question

Micael Karlberg <>
Wed Jul 17 17:23:06 CEST 2002


In article <>, Francesco Cesarini wrote:
> Hi Eric,
> welcome to the world of Erlang.
> 
> I assume the problem you have stumbled across is the usage of gen_tcp in 
> passive modes, making your gen_server hang until receive the request. 
> (The same applies to receiving data). You will be hanging, and thus 
> unable to handle code upgrades or other requests sent to the server. You 
> could use the timeout, but that will generate an unnecessary overhead.
> 
> Your solution is to spawn a process (Not following any behaviors), which 
> hangs in the call gen_tcp:accept/1 waiting for a connection request. 
> Make sure this process is linked to your gen_server. When you receive a 
> request, make your gen_server the controlling process and receive your 
> packages in active mode using the handle_info call.

There is a couple of examples of this in OTP, such as the megaco 
application (and inets-3.0). The Megaco application TCP transport 
has a very simple process that does exactly this, except that it 
is handled by a supervisor. The listen socket itself is created 
and owned by an other process.

> 
> Should you want to implement code upgrades, make sure you set the 
> timeout to a very low value. This will kill your listener process. Just 
> make sure you respawn it after the upgrade.

The process that hangs in accept is so simple that it should very
rarely need to be upgraded. If it does need to be upgraded, just 
kill it and it will be restarted by the supervisor, with the new 
code.

> 
> Hope the above helps. Good documentation on OTP design principles is at 
> http://www.erlang.org/doc/r8b/doc/design_principles/part_frame.html .
> 
> Mickael Remond & I wrote a paper on the "non" usage of OTP outside 
> Ericsson you might find interesting. You can read about at 
> http://www.erlang-consulting.com/euc2001/index.htm
> 
> Good Luck,
> Francesco
> --
> http://www.erlang-consulting.com
> 
> 

/BMK

> 
> Eric Merritt wrote:
> 
>>Guys,
>>
>> As some of you may know I am fairly new to erlang. I
>>have been poking around a bit getting my feet wet,
>>messing with yaws, mnesia, etc. Well I am finally
>>getting around to coding a fairly heft application as
>>a learning exercise and I have hit a stumbling block.
>>I am just begining to write the tcp comm part of the
>>application, I would love to follow the otp practices
>>and make use of gen_server. There even seems to be
>>some arcane way for gen_tcp and gen_server to work
>>together for a module. I can't, for the life of me,
>>figure it out. The documentation is pretty sparse in
>>this area as well. 
>>
>>
>> If I missed some documentation somewhere could one of
>>you point it out or if I missed something obvious just
>>let me know my error. Otherwise a little example would
>>be great. 
>>
>>I have spent a significant amount of time searching
>>the archives, the net, etc. Also I have attempted to
>>look at the inets and yaws source to figure this out.
>>The only problem there is that both the inets source
>>and the yaws source is largly undocumented. So I tend
>>to just get a headache accompanied by very little
>>understanding.
>>
>>So if one of you could point me in the right direction
>>that would be great. Otherwise I may have just
>>revealed the true magnitude of my ingnorance :)
>>
>>Thanks,
>>Eric
>>
>>__________________________________________________
>>Do You Yahoo!?
>>Yahoo! Autos - Get free new car price quotes
>>http://autos.yahoo.com
>>
>>
> 
> 


-- 
Micael Karlberg             Mail: 
Ericsson AB, Älvsjö Sweden  EAB/UHK/KD 



More information about the erlang-questions mailing list