Skip to content

Commit

Permalink
raise gun version to 2.1.0, fix gun options
Browse files Browse the repository at this point in the history
  • Loading branch information
sedinin committed Apr 19, 2024
1 parent 8ca1c1b commit e29a23a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
%% == Dependencies ==

{deps, [
{gun, "1.3.3"},
{gun, "2.1.0"},
{jsx, "3.1.0"},
{base64url, "1.0.1"}
]}.
Expand Down
43 changes: 19 additions & 24 deletions src/apns_connection.erl
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,14 @@ open_connection(internal, _, #{connection := Connection} = StateData) ->
open_origin(internal, _, #{connection := Connection} = StateData) ->
Host = host(Connection),
Port = port(Connection),
TransportOpts = transport_opts(Connection),
TlsOpts = tls_opts(Connection),
Http2Opts = http2_opts(),
{next_state, open_common, StateData,
{next_event, internal, { Host
, Port
, #{ protocols => [http2]
, http2_opts => TransportOpts
, http2_opts => Http2Opts
, tls_opts => TlsOpts
, retry => 0
}}}}.

Expand Down Expand Up @@ -289,12 +291,14 @@ proxy_connect_to_origin(internal, on_connect, StateData) ->
#{connection := Connection, gun_pid := GunPid} = StateData,
Host = host(Connection),
Port = port(Connection),
TransportOpts = transport_opts(Connection),
TlsOpts = tls_opts(Connection),
Http2Opts = http2_opts(),
Destination0 = #{ host => Host
, port => Port
, protocol => http2
, http2_opts => Http2Opts
, transport => tls
, tls_opts => TransportOpts
, tls_opts => TlsOpts
},
Destination = case proxy(Connection) of
#{ username := Username, password := Password } ->
Expand Down Expand Up @@ -569,29 +573,30 @@ proxy(_) ->

-spec default_max_gun_streams(connection()) -> non_neg_integer() | infinity.
default_max_gun_streams(Setts) ->
case type(Setts) of
token -> 1; %% at start, for token we should set 1
_ -> 100
end.

case type(Setts) of
token -> 1; %% at start, for token we should set 1
_ -> 100
end.

transport_opts(Connection) ->
tls_opts(Connection) ->
case type(Connection) of
certdata ->
Cert = certdata(Connection),
Key = keydata(Connection),
%% XXX: why is proplist here?
%% proplist here, because it goes to ssl:connect/3 (by gun)
[{cert, Cert}, {key, Key}];
cert ->
Certfile = certfile(Connection),
Keyfile = keyfile(Connection),
%% XXX: why is proplist here?
[{certfile, Certfile}, {keyfile, Keyfile}];
token ->
%% we need to know settings, http2 opt
#{notify_settings_changed => true}
[]
end.

http2_opts() ->
%% we need to know settings (from APN server), gun expects map
#{notify_settings_changed => true}.

%%%===================================================================
%%% Internal Functions
%%%===================================================================
Expand Down Expand Up @@ -637,16 +642,6 @@ send_push(GunPid, DeviceId, HeadersMap, Notification) ->
Path = get_device_path(DeviceId),
gun:post(GunPid, Path, Headers, Notification).

%% case gun:await(GunPid, StreamRef, Timeout) of
%% {response, fin, Status, ResponseHeaders} ->
%% {Status, ResponseHeaders, no_body};
%% {response, nofin, Status, ResponseHeaders} ->
%% {ok, Body} = gun:await_body(GunPid, StreamRef, Timeout),
%% DecodedBody = jsx:decode(Body, [{return_maps, false}]),
%% {Status, ResponseHeaders, DecodedBody};
%% {error, timeout} -> timeout
%% end.

-spec backoff(non_neg_integer(), non_neg_integer()) -> non_neg_integer().
backoff(N, Ceiling) ->
case (math:pow(2, N) - 1) of
Expand Down

0 comments on commit e29a23a

Please sign in to comment.