Skip to content

Commit

Permalink
feat: configurable ping_period for FCM workers
Browse files Browse the repository at this point in the history
  • Loading branch information
hpopp committed Aug 1, 2017
1 parent b1a818a commit 6f8b6f3
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
4 changes: 2 additions & 2 deletions lib/pigeon/apns.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
3 changes: 2 additions & 1 deletion lib/pigeon/fcm.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 6 additions & 3 deletions lib/pigeon/fcm/worker.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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],
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions test/fcm_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f8b6f3

Please sign in to comment.