emqx_session_router_worker_sup.erl 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_session_router_worker_sup).
  17. -behaviour(supervisor).
  18. -export([start_link/1]).
  19. -export([
  20. abort_worker/1,
  21. start_worker/2
  22. ]).
  23. -export([init/1]).
  24. start_link(SessionTab) ->
  25. supervisor:start_link({local, ?MODULE}, ?MODULE, SessionTab).
  26. start_worker(SessionID, RemotePid) ->
  27. supervisor:start_child(?MODULE, [
  28. #{
  29. session_id => SessionID,
  30. remote_pid => RemotePid
  31. }
  32. ]).
  33. abort_worker(Pid) ->
  34. supervisor:terminate_child(?MODULE, Pid).
  35. %%--------------------------------------------------------------------
  36. %% Supervisor callbacks
  37. %%--------------------------------------------------------------------
  38. init(SessionTab) ->
  39. %% Resume worker
  40. Worker = #{
  41. id => session_router_worker,
  42. start => {emqx_session_router_worker, start_link, [SessionTab]},
  43. restart => transient,
  44. shutdown => 2000,
  45. type => worker,
  46. modules => [emqx_session_router_worker]
  47. },
  48. Spec = #{
  49. strategy => simple_one_for_one,
  50. intensity => 1,
  51. period => 5
  52. },
  53. {ok, {Spec, [Worker]}}.