Skip to content

Commit

Permalink
Add runtime callback for 'pingreq' messages (#24)
Browse files Browse the repository at this point in the history
* Add runtime callback for 'pingreq' messages

* CI: remove OTP 24, add OTP 27
  • Loading branch information
mworrell authored Oct 9, 2024
1 parent 413b6b4 commit 03b2c34
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

strategy:
matrix:
otp_version: [24, 25, 26]
otp_version: [25, 26, 27]
os: [ubuntu-latest]

container:
Expand Down
6 changes: 4 additions & 2 deletions src/mqtt_sessions_process.erl
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,10 @@ handle_incoming(#{ type := subscribe } = Msg, _Options, State) ->
handle_incoming(#{ type := unsubscribe } = Msg, _Options, State) ->
packet_unsubscribe(Msg, State);

handle_incoming(#{ type := pingreq }, _Options, State) ->
State1 = reply_or_drop(#{ type => pingresp }, State),
handle_incoming(#{ type := pingreq } = Msg, _Options, State) ->
#state{ runtime = Runtime, user_context = UserContext } = State,
{ok, UserContext1} = Runtime:ping(UserContext),
State1 = reply_or_drop(#{ type => pingresp }, State#state{ user_context = UserContext1 }),
{ok, State1};
handle_incoming(#{ type := pingresp }, _Options, State) ->
{ok, State};
Expand Down
7 changes: 7 additions & 0 deletions src/mqtt_sessions_runtime.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
new_user_context/3,
connect/4,
control_message/3,
ping/1,
reauth/2,
is_allowed/4,
is_valid_message/3
Expand Down Expand Up @@ -152,6 +153,12 @@ connect(_Packet, _IsSessionPresent, _Options, UserContext) ->
reauth(#{ type := auth }, _UserContext) ->
{error, notsupported}.

%% @doc Called on a pingreq message from the remote. Used this to keep processes or track connection status.
-spec ping(UserContext) -> {ok, UserContext1} when
UserContext :: user_context(),
UserContext1 :: user_context().
ping(UserContext) ->
{ok, UserContext}.

-spec is_allowed( publish | subscribe, topic(), mqtt_packet_map:mqtt_packet(), user_context()) -> boolean().
is_allowed(publish, _Topic, _Packet, _UserContext) ->
Expand Down

0 comments on commit 03b2c34

Please sign in to comment.