From a7c767cff8fe223eb8e40a83767822b49fcda125 Mon Sep 17 00:00:00 2001 From: Michael Smykowski Date: Thu, 28 Jan 2021 14:48:51 -0700 Subject: [PATCH 1/2] Handle when socket is nil --- lib/pigeon/connection.ex | 16 ++++++++++++++++ test/adm/config_test.exs | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/pigeon/connection.ex b/lib/pigeon/connection.ex index f0f04adb..58f902d9 100644 --- a/lib/pigeon/connection.ex +++ b/lib/pigeon/connection.ex @@ -92,6 +92,22 @@ defmodule Pigeon.Connection do end end + def handle_events(events, from, %{config: config, socket: nil} = state) do + case connect_socket(config, 0) do + {:ok, socket} -> + Configurable.schedule_ping(config) + + handle_events(events, from, %Connection{ + config: state.config, + from: state.from, + socket: socket + }) + + {:error, reason} -> + {:stop, reason, state} + end + end + def handle_events(events, _from, state) do state = Enum.reduce(events, state, fn {:push, notif, opts}, state -> diff --git a/test/adm/config_test.exs b/test/adm/config_test.exs index 8120497e..69df9857 100644 --- a/test/adm/config_test.exs +++ b/test/adm/config_test.exs @@ -5,8 +5,14 @@ defmodule Pigeon.ADM.ConfigTest do @invalid_key_msg ~r/^attempted to start without valid client id and secret/ test "success if configured with client id and secret" do - config = Pigeon.ADM.Config.new(name: :test, client_id: "amzn1.iba-client.abc123", client_secret: "abc123") - {:ok, _} = Pigeon.ADM.Worker.init({:ok, config}) + config = + Pigeon.ADM.Config.new( + name: :test, + client_id: "amzn1.iba-client.abc123", + client_secret: "abc123" + ) + + {:ok, _} = Pigeon.ADM.Worker.init({:ok, config}) end test "raises if configured with invalid client id" do @@ -18,7 +24,13 @@ defmodule Pigeon.ADM.ConfigTest do test "raises if configured with invalid client secret" do assert_raise(Pigeon.ConfigError, @invalid_key_msg, fn -> - config = Pigeon.ADM.Config.new(name: :test, client_id: "amzn1.iba-client.abc123", client_secret: nil) + config = + Pigeon.ADM.Config.new( + name: :test, + client_id: "amzn1.iba-client.abc123", + client_secret: nil + ) + Pigeon.ADM.Worker.init({:ok, config}) end) end From da8f48010739d9f8a3c95b41688241b909c6ed8d Mon Sep 17 00:00:00 2001 From: Michael Smykowski Date: Tue, 23 Feb 2021 19:51:59 -0700 Subject: [PATCH 2/2] Check if a connection is alive before sending a push --- lib/pigeon/connection.ex | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/lib/pigeon/connection.ex b/lib/pigeon/connection.ex index 58f902d9..9ead6670 100644 --- a/lib/pigeon/connection.ex +++ b/lib/pigeon/connection.ex @@ -92,31 +92,31 @@ defmodule Pigeon.Connection do end end - def handle_events(events, from, %{config: config, socket: nil} = state) do - case connect_socket(config, 0) do - {:ok, socket} -> - Configurable.schedule_ping(config) - - handle_events(events, from, %Connection{ - config: state.config, - from: state.from, - socket: socket - }) - - {:error, reason} -> - {:stop, reason, state} + def handle_events(events, from, %{config: config, socket: socket} = state) do + if socket && Process.alive?(socket) do + state = + Enum.reduce(events, state, fn {:push, notif, opts}, state -> + send_push(state, notif, opts) + end) + + {:noreply, [], state} + else + case connect_socket(config, 0) do + {:ok, socket} -> + Configurable.schedule_ping(config) + + handle_events(events, from, %Connection{ + config: state.config, + from: state.from, + socket: socket + }) + + {:error, reason} -> + {:stop, reason, state} + end end end - def handle_events(events, _from, state) do - state = - Enum.reduce(events, state, fn {:push, notif, opts}, state -> - send_push(state, notif, opts) - end) - - {:noreply, [], state} - end - def process_end_stream(%Stream{id: stream_id} = stream, state) do %{queue: queue, config: config} = state