Skip to content

Commit 03b2c34

Browse files
authored
Add runtime callback for 'pingreq' messages (#24)
* Add runtime callback for 'pingreq' messages * CI: remove OTP 24, add OTP 27
1 parent 413b6b4 commit 03b2c34

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818

1919
strategy:
2020
matrix:
21-
otp_version: [24, 25, 26]
21+
otp_version: [25, 26, 27]
2222
os: [ubuntu-latest]
2323

2424
container:

src/mqtt_sessions_process.erl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,10 @@ handle_incoming(#{ type := subscribe } = Msg, _Options, State) ->
416416
handle_incoming(#{ type := unsubscribe } = Msg, _Options, State) ->
417417
packet_unsubscribe(Msg, State);
418418

419-
handle_incoming(#{ type := pingreq }, _Options, State) ->
420-
State1 = reply_or_drop(#{ type => pingresp }, State),
419+
handle_incoming(#{ type := pingreq } = Msg, _Options, State) ->
420+
#state{ runtime = Runtime, user_context = UserContext } = State,
421+
{ok, UserContext1} = Runtime:ping(UserContext),
422+
State1 = reply_or_drop(#{ type => pingresp }, State#state{ user_context = UserContext1 }),
421423
{ok, State1};
422424
handle_incoming(#{ type := pingresp }, _Options, State) ->
423425
{ok, State};

src/mqtt_sessions_runtime.erl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
new_user_context/3,
2626
connect/4,
2727
control_message/3,
28+
ping/1,
2829
reauth/2,
2930
is_allowed/4,
3031
is_valid_message/3
@@ -152,6 +153,12 @@ connect(_Packet, _IsSessionPresent, _Options, UserContext) ->
152153
reauth(#{ type := auth }, _UserContext) ->
153154
{error, notsupported}.
154155

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

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

0 commit comments

Comments
 (0)