Permalink
Browse files

Move the receiver and sender into the pid of `control`.

This means an error in one of those processes will take down the whole tree. Since they
are marked as temporary, it should not produce any kind of message anymore when/if they
go down.
  • Loading branch information...
1 parent 02438fc commit f1a15131fd666387b265955e10532dfe6cd1f1d4 @jlouis committed Oct 20, 2012
Showing with 16 additions and 81 deletions.
  1. +3 −0 src/etorrent_peer_control.erl
  2. +13 −19 src/etorrent_peer_pool.erl
  3. +0 −62 src/etorrent_peer_sup.erl
View
3 src/etorrent_peer_control.erl
@@ -293,6 +293,9 @@ init([TrackerUrl, LocalPeerID, InfoHash, TorrentID, {IP, Port}, Caps, Socket]) -
ok = etorrent_table:new_peer(TrackerUrl, IP, Port, TorrentID, self(), leeching),
ok = etorrent_choker:monitor(self()),
+
+ {ok, RecieverPid} = etorrent_peer_recv:start_link(Id, Socket),
+ {ok, SenderPid} = etorrent_peer_send:start_link(Id, Socket, false),
State = #state{
torrent_id=TorrentID,
info_hash=InfoHash,
View
32 src/etorrent_peer_pool.erl
@@ -44,29 +44,23 @@ start_child(PeerId, InfoHash, Id, {IP, Port}, Capabilities, Socket) ->
{ok, pid(), pid()} | {error, term()}.
start_child(TrackerUrl, PeerId, InfoHash, Id,
{IP, Port}, Capabilities, Socket) ->
- GroupPid = gproc:lookup_local_name({torrent, Id, peer_pool_sup}),
- case supervisor:start_child(GroupPid,
- [TrackerUrl,
- PeerId, InfoHash,
- Id,
- {IP, Port},
- Capabilities,
- Socket]) of
- {ok, _Pid} ->
- RecvPid = etorrent_peer_recv:await_server(Socket),
- ControlPid = etorrent_peer_control:await_server(Socket),
- {ok, RecvPid, ControlPid};
- {error, Reason} ->
- lager:warning("Error starting child: ~p", [Reason]),
- {error, Reason}
+ GroupPid = gproc:lookup_local_name({torrent, Id, peer_pool_sup}),
+ case supervisor:start_child(GroupPid, [TrackerUrl, PeerId, InfoHash, Id, {IP, Port}, Capabilities, Socket]) of
+ {ok, _Pid} ->
+ RecvPid = etorrent_peer_recv:await_server(Socket),
+ ControlPid = etorrent_peer_control:await_server(Socket),
+ {ok, RecvPid, ControlPid}
+ {error, Reason} ->
+ lager:warning("Error starting child: ~p", [Reason]),
+ {error, Reason}
end.
%% ====================================================================
%% @private
init([Id]) ->
gproc:add_local_name({torrent, Id, peer_pool_sup}),
- ChildSpec = {child,
- {etorrent_peer_sup, start_link, []},
- temporary, infinity, supervisor, [etorrent_peer_sup]},
- {ok, {{simple_one_for_one, 50, 3600}, [ChildSpec]}}.
+ ChildSpec = {child, {
+ {etorrent_peer_control, start_link, []},
+ temporary, 5000, worker, [etorrent_peer_control]},
+ {ok, {{simple_one_for_one, 10, 3600}, [ChildSpec]}}.
View
62 src/etorrent_peer_sup.erl
@@ -1,62 +0,0 @@
-%% @author Jesper Louis Andersen <jesper.louis.andersen@gmail.com>
-%% @doc Supervise a peer
-%% <p>This module represents a peer. It spawns a supervisor, which in
-%% turn spawns 3 gen_servers: one for sending, one for receiving and
-%% one for control.</p>
-%% <p>The supervisor has a very aggressive termination policy. Any
-%% error will terminate the peer totally. This is deliberate: we have
-%% other peers we could try, so if there is an error with this peer,
-%% it shouldn't really try to keep it around. We'll just try another.</p>
-%% @end
--module(etorrent_peer_sup).
--behaviour(supervisor).
-
-%% API
--export([start_link/7]).
-
-%% Supervisor callbacks
--export([init/1]).
-
--define(SERVER, ?MODULE).
-
--type ipaddr() :: etorrent_types:ipaddr().
--type portnum() :: etorrent_types:portnum().
--type capabilities() :: etorrent_types:capabilities().
-
-%% ====================================================================
-
-%% @doc Start the peer
-%% <p>A peer is fed with quite a lot of data. It gets tracker url, our
-%% local `PeerId', it gets the `InfoHash', the Torrents `Id', the pair
-%% `{IP, Port}` of the remote peer, what `Capabilities' the peer supports
-%% and a `Socket' for communication.</p>
-%% <p>From that a supervisor for the peer and accompanying processes
-%% are spawned.</p>
-%% @end
--spec start_link(string(), binary(), binary(), integer(), {ipaddr(), portnum()},
- [capabilities()], port()) ->
- {ok, pid()} | ignore | {error, term()}.
-start_link(TrackerUrl, LocalPeerId, InfoHash, Id, {IP, Port}, Capabilities, Socket) ->
- supervisor:start_link(?MODULE, [TrackerUrl,
- LocalPeerId,
- InfoHash,
- Id,
- {IP, Port},
- Capabilities,
- Socket]).
-
-%% ====================================================================
-
-%% @private
-init([TrackerUrl, LocalPeerId, InfoHash, Id, {IP, Port}, Caps, Socket]) ->
- Control = {control, {etorrent_peer_control, start_link,
- [TrackerUrl, LocalPeerId, InfoHash, Id, {IP, Port},
- Caps, Socket]},
- transient, 5000, worker, [etorrent_peer_control]},
- Receiver = {receiver, {etorrent_peer_recv, start_link,
- [Id, Socket]},
- transient, 5000, worker, [etorrent_peer_recv]},
- Sender = {sender, {etorrent_peer_send, start_link,
- [Socket, Id, false]},
- transient, 5000, worker, [etorrent_peer_send]},
- {ok, {{one_for_all, 0, 1}, [Control, Sender, Receiver]}}.

0 comments on commit f1a1513

Please sign in to comment.