summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorPaul Schoenfelder <paulschoenfelder@gmail.com>2016-05-16 09:16:01 -0500
committerPaul Schoenfelder <paulschoenfelder@gmail.com>2016-05-16 09:16:01 -0500
commitbddcdc8160589c0fdbf79d3f64597849c520f750 (patch)
tree425692b1bd46dae8efe168959719c69cdffecf20 /lib
parentMerge pull request #43 from Annwenn/master (diff)
parentStart connections as temporary children (diff)
Merge pull request #50 from michalmuskala/monitor-owner
Prevent zombie connections
Diffstat (limited to 'lib')
-rw-r--r--lib/exirc/client.ex13
-rw-r--r--lib/exirc/exirc.ex4
2 files changed, 13 insertions, 4 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..b32af54 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
##############
@@ -60,7 +60,7 @@ defmodule ExIrc do
@spec init(any) :: {:ok, pid} | {:error, term}
def init(_) do
children = [
- worker(ExIrc.Client, [], restart: :transient)
+ worker(ExIrc.Client, [], restart: :temporary)
]
supervise children, strategy: :simple_one_for_one
end