emqx_batch.erl 2.7 KB

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