/src/global_distrib/mod_global_distrib_worker.erl

https://github.com/esl/MongooseIM · Erlang · 87 lines · 44 code · 18 blank · 25 comment · 0 complexity · 9dc6e0c30b723e2bba29558d62318f9e MD5 · raw file

  1. %%==============================================================================
  2. %% Copyright 2017 Erlang Solutions Ltd.
  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. %% Routing worker
  17. -module(mod_global_distrib_worker).
  18. -author('konrad.zemek@erlang-solutions.com').
  19. -behaviour(gen_server).
  20. -include("mongoose.hrl").
  21. -include("jlib.hrl").
  22. -include("global_distrib_metrics.hrl").
  23. -define(TIMEOUT, 60000). % TODO
  24. -export([start_link/1]).
  25. -export([init/1, handle_call/3, handle_cast/2, handle_info/2, code_change/3, terminate/2]).
  26. -ignore_xref([start_link/1]).
  27. %%--------------------------------------------------------------------
  28. %% API
  29. %%--------------------------------------------------------------------
  30. -spec start_link(Name :: atom()) -> {ok, pid()} | {error, any()}.
  31. start_link(Name) ->
  32. gen_server:start_link({local, Name}, ?MODULE, [], []).
  33. %%--------------------------------------------------------------------
  34. %% gen_server API
  35. %%--------------------------------------------------------------------
  36. init(_) ->
  37. {ok, state, ?TIMEOUT}.
  38. handle_call({data, Host, TransferTime, Stamp, Data}, From, State) ->
  39. gen_server:reply(From, ok),
  40. handle_cast({data, Host, TransferTime, Stamp, Data}, State).
  41. handle_cast({route, {From, To, Acc, Packet}}, State) ->
  42. ejabberd_router:route(From, To, Acc, Packet),
  43. {noreply, State, ?TIMEOUT};
  44. handle_cast({data, Host, TransferTime, Stamp, Data}, State) ->
  45. QueueTimeNative = erlang:monotonic_time() - Stamp,
  46. QueueTimeUS = erlang:convert_time_unit(QueueTimeNative, native, microsecond),
  47. mongoose_metrics:update(global, ?GLOBAL_DISTRIB_RECV_QUEUE_TIME, QueueTimeUS),
  48. mongoose_metrics:update(global, ?GLOBAL_DISTRIB_TRANSFER_TIME(Host), TransferTime),
  49. mongoose_metrics:update(global, ?GLOBAL_DISTRIB_MESSAGES_RECEIVED(Host), 1),
  50. do_work(Data),
  51. {noreply, State, ?TIMEOUT}.
  52. handle_info(timeout, State) ->
  53. {stop, {shutdown, spinning_down_idle_worker}, State};
  54. handle_info(_, State) ->
  55. {noreply, State, ?TIMEOUT}.
  56. code_change(_FromVersion, State, _Extra) ->
  57. {ok, State}.
  58. terminate(_Reason, _State) ->
  59. ok.
  60. %%--------------------------------------------------------------------
  61. %% Helpers
  62. %%--------------------------------------------------------------------
  63. -spec do_work(Data :: binary()) -> any().
  64. do_work(Data) ->
  65. {From, To, Acc, Packet} = erlang:binary_to_term(Data),
  66. mod_global_distrib_utils:maybe_update_mapping(From, Acc),
  67. ?LOG_DEBUG(#{what => gd_route_incoming, acc => Acc,
  68. gd_id => mod_global_distrib:get_metadata(Acc, id, "unknown")}),
  69. ejabberd_router:route(From, To, Acc, Packet).