emqx_batch.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2019 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_batch).
  17. %% APIs
  18. -export([ init/1
  19. , push/2
  20. , commit/1
  21. , size/1
  22. , items/1
  23. ]).
  24. -export_type([options/0, batch/0]).
  25. -record(batch, {
  26. batch_size :: non_neg_integer(),
  27. batch_q :: list(any()),
  28. linger_ms :: pos_integer(),
  29. linger_timer :: reference() | undefined,
  30. commit_fun :: function()
  31. }).
  32. -type(options() :: #{
  33. batch_size => non_neg_integer(),
  34. linger_ms => pos_integer(),
  35. commit_fun := function()
  36. }).
  37. -opaque(batch() :: #batch{}).
  38. %%--------------------------------------------------------------------
  39. %% APIs
  40. %%--------------------------------------------------------------------
  41. -spec(init(options()) -> batch()).
  42. init(Opts) when is_map(Opts) ->
  43. #batch{batch_size = maps:get(batch_size, Opts, 1000),
  44. batch_q = [],
  45. linger_ms = maps:get(linger_ms, Opts, 1000),
  46. commit_fun = maps:get(commit_fun, Opts)}.
  47. -spec(push(any(), batch()) -> batch()).
  48. push(El, Batch = #batch{batch_q = Q,
  49. linger_ms = Ms,
  50. linger_timer = undefined})
  51. when length(Q) == 0 ->
  52. TRef = erlang:send_after(Ms, self(), batch_linger_expired),
  53. Batch#batch{batch_q = [El], linger_timer = TRef};
  54. %% no limit.
  55. push(El, Batch = #batch{batch_size = 0, batch_q = Q}) ->
  56. Batch#batch{batch_q = [El|Q]};
  57. push(El, Batch = #batch{batch_size = MaxSize, batch_q = Q})
  58. when length(Q) >= MaxSize ->
  59. commit(Batch#batch{batch_q = [El|Q]});
  60. push(El, Batch = #batch{batch_q = Q}) ->
  61. Batch#batch{batch_q = [El|Q]}.
  62. -spec(commit(batch()) -> batch()).
  63. commit(Batch = #batch{batch_q = Q, commit_fun = Commit}) ->
  64. _ = Commit(lists:reverse(Q)),
  65. reset(Batch).
  66. reset(Batch = #batch{linger_timer = TRef}) ->
  67. _ = emqx_misc:cancel_timer(TRef),
  68. Batch#batch{batch_q = [], linger_timer = undefined}.
  69. -spec(size(batch()) -> non_neg_integer()).
  70. size(#batch{batch_q = Q}) ->
  71. length(Q).
  72. -spec(items(batch()) -> list(any())).
  73. items(#batch{batch_q = Q}) ->
  74. lists:reverse(Q).