Erlang/OTP Forums

Author Message

<  Advanced Erlang/OTP  ~  Closing down streaming tcp server

Coll
Posted: Tue Jul 31, 2007 11:11 am Reply with quote
Joined: 31 Jul 2007 Posts: 6
Hi,

New here, don't know if this is the right forum for this question but...

I haven't found any good information on this.

I have a process streaming data constantly using gen_tcp, right now I spawn this process with erlang:spawn(fun server:start/0).

Then it waits for a connection and start streaming when someone connect.

Everything else in the system is OTP with supervisors and gen_servers.

Now, my problem is that I want some way to nicely shut this process down when the systems shuts down or I for some other reason wants to stop streaming, do clean up (closing socket).

It works if the client connected disconnect, but sometimes I want to shut it down nicely even if the client haven't disconnected.

I guess I could use a receive after 0 and see if I get a message to shutdown between every packet I send over TCP, but I would prefer not to, something tells me it's not very nice to poll my messages all the time. Is there any other way?
View user's profile Send private message
Mazen
Posted: Wed Aug 01, 2007 8:31 am Reply with quote
User Joined: 20 Jul 2006 Posts: 164 Location: London
Hi Coll,

Hmmm tricky one... of course the easiest way would be to actually check your message box every time but if you want to overengineer a little this might work.

Assume you have 1 server process working as a process manager for processes that stream data. The process for streaming data does only that, it streams data, the manager process takes care of the post- and/or pre apply.

so... if first process is a gen server... I could think that the following sequence of actions would be taken.

1) call gen server to start a new stream process with a few args
2) gen server process prepares and starts the process and saves the Pid in a list somewhere
3) call gen server to stop the stream process
4) gen server process prepares and then kills the streaming process.
5) gen server process then cleans up after streaming process

As I said... a little overengineered...

Just thought of something else now... don't know how "beautiful" or "ugly" this is... but you could send a message to yourself..
Code:
self() ! msg.

then every loop you patternmatch for either this message or the stop message.

but after all... after 0 seems the easiest way Smile
View user's profile Send private message
francesco
Posted: Wed Aug 01, 2007 11:33 am Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Hi Coll, welcome to trapexit. You've found the right forum.

Quote:
I have a process streaming data constantly using gen_tcp, right now I spawn this process with erlang:spawn(fun server:start/0).


You should use spawn_link instead of spawn, ensuring that the process taking care of the streaming is started by a process in your supervision tree. When you shut down the system (or just the application), this process will be terminated together with all other processes linked to it.

Try it out,
Francesco
View user's profile Send private message Visit poster's website
Coll
Posted: Wed Aug 01, 2007 12:11 pm Reply with quote
Joined: 31 Jul 2007 Posts: 6
Thanks for the answers.

But even if I use spawn link wouldn't the socket still be open even if the process streaming (using the socket) is terminated?

I want to close it properly.
View user's profile Send private message
francesco
Posted: Wed Aug 01, 2007 5:18 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Sockets use ports. They are made to act as processes not trapping exits. This means they can be linked to, can send and receive messages and react on exit signals. So if your process is linked to it and it terminates, the exit signal will kill the port/socket.

See: http://www.erlang.org/course/advanced.html#ports for more information. I had a look at the gen_tcp module, and it was not well documented. The above is the best I could find. It should take you a few minutes to hack together a test and proove / disproove the above.
View user's profile Send private message Visit poster's website
Coll
Posted: Thu Aug 02, 2007 12:15 pm Reply with quote
Joined: 31 Jul 2007 Posts: 6
I see, thanks for the replies.
View user's profile Send private message
francesco
Posted: Mon Aug 06, 2007 7:09 pm Reply with quote
User Joined: 07 Jul 2006 Posts: 249 Location: London
Did you see this tutorial which was added last night? It might help
http://www.trapexit.org/index.php/Building_a_Non-blocking_TCP_server_using_OTP_principles

Francesco
--
http://www.erlang-consulting.com
View user's profile Send private message Visit poster's website
Coll
Posted: Tue Aug 07, 2007 10:33 am Reply with quote
Joined: 31 Jul 2007 Posts: 6
Yes, I saw the link on the first page, haven't had time to read it through properly yet but I glanced at it and it seems good.
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum