From 6f8b6f34273fcee272b83130e3b6b9b449c8e9fe Mon Sep 17 00:00:00 2001 From: Henry Popp Date: Mon, 31 Jul 2017 22:36:45 -0500 Subject: [PATCH] feat: configurable ping_period for FCM workers --- CHANGELOG.md | 3 +++ lib/pigeon/apns.ex | 4 ++-- lib/pigeon/fcm.ex | 3 ++- lib/pigeon/fcm/worker.ex | 9 ++++++--- mix.exs | 2 +- test/fcm_test.exs | 12 ++++++++++++ 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e550f045..ef962da2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Changelog +## v1.0.1 +* Configurable `:ping_period` for FCM connections + ## v1.0.0 * GCM migrated to FCM API (http2) * `GCM` modules renamed to `FCM` diff --git a/lib/pigeon/apns.ex b/lib/pigeon/apns.ex index b03a8969..b495051a 100644 --- a/lib/pigeon/apns.ex +++ b/lib/pigeon/apns.ex @@ -75,11 +75,11 @@ defmodule Pigeon.APNS do @doc """ Sends a push over APNS. """ - @spec push([Notification.t], ((Notification.t) -> ()), Keyword.t) :: no_return + @spec push([Notification.t], ((Notification.t) -> no_return), Keyword.t) :: no_return def push(notification, on_response, opts) when is_list(notification) do for n <- notification, do: push(n, on_response, opts) end - @spec push(Notification.t, ((Notification.t) -> ()), Keyword.t) :: no_return + @spec push(Notification.t, ((Notification.t) -> no_return), Keyword.t) :: no_return def push(notification, on_response, opts) do worker_name = opts[:to] || Config.default_name GenServer.cast(worker_name, {:push, :apns, notification, on_response}) diff --git a/lib/pigeon/fcm.ex b/lib/pigeon/fcm.ex index 8486f389..fc8a4e71 100644 --- a/lib/pigeon/fcm.ex +++ b/lib/pigeon/fcm.ex @@ -99,7 +99,8 @@ defmodule Pigeon.FCM do def start_connection(opts) do config = %{ name: opts[:name], - key: opts[:key] + key: opts[:key], + ping_period: opts[:ping_period] } Pigeon.FCM.Worker.start_link(config) end diff --git a/lib/pigeon/fcm/worker.ex b/lib/pigeon/fcm/worker.ex index 8979b4e8..7dac91b1 100644 --- a/lib/pigeon/fcm/worker.ex +++ b/lib/pigeon/fcm/worker.ex @@ -7,7 +7,7 @@ defmodule Pigeon.FCM.Worker do alias Pigeon.FCM.{NotificationResponse, ResultParser} - @ping_period 600_000 # 10 minutes + @default_ping_period 600_000 # 10 minutes defp fcm_uri(config), do: config[:endpoint] || 'fcm.googleapis.com' @@ -28,7 +28,10 @@ defmodule Pigeon.FCM.Worker do def initialize_worker(config) do case connect_socket(config, 0) do {:ok, socket} -> - Process.send_after(self(), :ping, @ping_period) + ping = config[:ping_period] || @default_ping_period + config = Map.put(config, :ping_period, ping) + Process.send_after(self(), :ping, ping) + {:ok, %{ socket: socket, key: config[:key], @@ -141,7 +144,7 @@ defmodule Pigeon.FCM.Worker do def handle_info(:ping, state) do Pigeon.Http2.Client.default().send_ping(state.socket) - Process.send_after(self(), :ping, @ping_period) + Process.send_after(self(), :ping, state.config.ping_period) {:noreply, state} end diff --git a/mix.exs b/mix.exs index 400391fe..e13a1bcb 100644 --- a/mix.exs +++ b/mix.exs @@ -4,7 +4,7 @@ defmodule Pigeon.Mixfile do def project do [app: :pigeon, name: "Pigeon", - version: "1.0.0", + version: "1.0.1", elixir: "~> 1.2", source_url: "https://github.com/codedge-llc/pigeon", description: description(), diff --git a/test/fcm_test.exs b/test/fcm_test.exs index fe68f27e..70c73f23 100644 --- a/test/fcm_test.exs +++ b/test/fcm_test.exs @@ -24,6 +24,18 @@ defmodule Pigeon.FCMTest do assert state.key == fcm_key assert is_pid(state.socket) end + + test "configures ping_period if specified" do + fcm_key = Application.get_env(:pigeon, :test)[:fcm_key] + opts = [ + key: fcm_key, + ping_period: 30_000 + ] + + {:ok, pid} = Pigeon.FCM.start_connection(opts) + state = :sys.get_state(pid) + assert state.config.ping_period == 30_000 + end end describe "push/1 with custom worker" do