diff options
author | Michal Muskala <michal@muskala.eu> | 2016-05-13 20:51:08 +0200 |
---|---|---|
committer | Michal Muskala <michal@muskala.eu> | 2016-05-13 21:25:14 +0200 |
commit | 01c4c42e117636cdba2d5278c851fbe7a3b39bc0 (patch) | |
tree | a8a6bbf1f15715e982c7d7c5aade7ac5593dc6f2 /lib | |
parent | Merge pull request #43 from Annwenn/master (diff) |
Monitor owner of the connection
The connection process should monitor the process that started it
and die with the same reason that the owner process did.
This should solve the issue of zombie connections laying around
after the processes that started them die.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/exirc/client.ex | 13 | ||||
-rw-r--r-- | lib/exirc/exirc.ex | 2 |
2 files changed, 12 insertions, 3 deletions
diff --git a/lib/exirc/client.ex b/lib/exirc/client.ex index cde4f1a..81a2f0a 100644 --- a/lib/exirc/client.ex +++ b/lib/exirc/client.ex @@ -32,7 +32,8 @@ defmodule ExIrc.Client do channels: [], debug?: false, retries: 0, - inet: :inet + inet: :inet, + owner: nil end ################# @@ -55,6 +56,7 @@ defmodule ExIrc.Client do """ @spec start_link(options :: list() | nil, process_opts :: list() | nil) :: {:ok, pid} | {:error, term} def start_link(options \\ [], process_opts \\ []) do + options = Keyword.put_new(options, :owner, self()) GenServer.start_link(__MODULE__, options, process_opts) end @doc """ @@ -285,17 +287,20 @@ defmodule ExIrc.Client do def init(options \\ []) do autoping = Keyword.get(options, :autoping, true) debug = Keyword.get(options, :debug, false) + owner = Keyword.fetch!(options, :owner) # Add event handlers handlers = Keyword.get(options, :event_handlers, []) |> List.foldl([], &do_add_handler/2) + ref = Process.monitor(owner) # Return initial state {:ok, %ClientState{ event_handlers: handlers, autoping: autoping, logged_on?: false, debug?: debug, - channels: ExIrc.Channels.init()}} + channels: ExIrc.Channels.init(), + owner: {owner, ref}}} end @doc """ Handle calls from the external API. It is not recommended to call these directly. @@ -497,6 +502,10 @@ defmodule ExIrc.Client do def handle_info({:ssl, socket, data}, state) do handle_info({:tcp, socket, data}, state) end + # If the owner process dies, we should die as well + def handle_info({:DOWN, ref, _, pid, reason}, %{owner: {pid, ref}} = state) do + {:stop, reason, state} + end # If an event handler process dies, remove it from the list of event handlers def handle_info({:DOWN, _, _, pid, _}, state) do handlers = do_remove_handler(pid, state.event_handlers) diff --git a/lib/exirc/exirc.ex b/lib/exirc/exirc.ex index cfa2654..72be33c 100644 --- a/lib/exirc/exirc.ex +++ b/lib/exirc/exirc.ex @@ -50,7 +50,7 @@ defmodule ExIrc do @spec start_client! :: {:ok, pid} | {:error, term} def start_client! do # Start the client worker - Supervisor.start_child(:exirc, []) + Supervisor.start_child(:exirc, [[owner: self()]]) end ############## |