emqx_resource.hrl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2024 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. %% bridge/connector/action status
  17. -define(status_connected, connected).
  18. -define(status_connecting, connecting).
  19. -define(status_disconnected, disconnected).
  20. %% Note: the `stopped' status can only be emitted by `emqx_resource_manager'... Modules
  21. %% implementing `emqx_resource' behavior should not return it. The `rm_' prefix is to
  22. %% remind us of that.
  23. -define(rm_status_stopped, stopped).
  24. -type resource_type() :: module().
  25. -type resource_id() :: binary().
  26. -type channel_id() :: binary().
  27. -type raw_resource_config() :: binary() | raw_term_resource_config().
  28. -type raw_term_resource_config() :: #{binary() => term()} | [raw_term_resource_config()].
  29. -type resource_config() :: term().
  30. -type resource_spec() :: map().
  31. -type resource_state() :: term().
  32. %% Note: the `stopped' status can only be emitted by `emqx_resource_manager'... Modules
  33. %% implementing `emqx_resource' behavior should not return it.
  34. -type resource_status() ::
  35. ?status_connected | ?status_disconnected | ?status_connecting | ?rm_status_stopped.
  36. -type health_check_status() :: ?status_connected | ?status_disconnected | ?status_connecting.
  37. -type channel_status() :: ?status_connected | ?status_connecting | ?status_disconnected.
  38. -type callback_mode() :: always_sync | async_if_possible.
  39. -type query_mode() ::
  40. simple_sync
  41. | simple_async
  42. | simple_sync_internal_buffer
  43. | simple_async_internal_buffer
  44. | sync
  45. | async
  46. | no_queries.
  47. -type result() :: term().
  48. -type reply_fun() ::
  49. {fun((...) -> any()), Args :: [term()]}
  50. | {fun((...) -> any()), Args :: [term()], reply_context()}
  51. | undefined.
  52. -type reply_context() :: #{reply_dropped => boolean()}.
  53. -type query_opts() :: #{
  54. %% The key used for picking a resource worker
  55. pick_key => term(),
  56. timeout => timeout(),
  57. expire_at => infinity | integer(),
  58. async_reply_fun => reply_fun(),
  59. simple_query => boolean(),
  60. reply_to => reply_fun(),
  61. query_mode => query_mode(),
  62. connector_resource_id => resource_id()
  63. }.
  64. -type resource_data() :: #{
  65. id := resource_id(),
  66. mod := module(),
  67. callback_mode := callback_mode(),
  68. query_mode := query_mode(),
  69. config := resource_config(),
  70. error := term(),
  71. state := resource_state(),
  72. status := resource_status(),
  73. added_channels := term()
  74. }.
  75. -type resource_group() :: binary().
  76. -type creation_opts() :: #{
  77. %%======================================= Deprecated Opts BEGIN
  78. %% use health_check_interval instead
  79. health_check_timeout => integer(),
  80. %% use start_timeout instead
  81. wait_for_resource_ready => integer(),
  82. %% use health_check_interval instead
  83. auto_retry_interval => integer(),
  84. %% use health_check_interval instead
  85. auto_restart_interval => pos_integer() | infinity,
  86. %%======================================= Deprecated Opts END
  87. worker_pool_size => non_neg_integer(),
  88. %% use `integer()` compatibility to release 5.0.0 bpapi
  89. health_check_interval => integer(),
  90. %% We can choose to block the return of emqx_resource:start until
  91. %% the resource connected, wait max to `start_timeout` ms.
  92. start_timeout => pos_integer(),
  93. %% If `start_after_created` is set to true, the resource is started right
  94. %% after it is created. But note that a `started` resource is not guaranteed
  95. %% to be `connected`.
  96. start_after_created => boolean(),
  97. batch_size => pos_integer(),
  98. batch_time => pos_integer(),
  99. max_buffer_bytes => pos_integer(),
  100. query_mode => query_mode(),
  101. resume_interval => pos_integer(),
  102. inflight_window => pos_integer(),
  103. %% Only for `emqx_resource_manager' usage. If false, prevents spawning buffer
  104. %% workers, regardless of resource query mode.
  105. spawn_buffer_workers => boolean()
  106. }.
  107. -type query_result() ::
  108. ok
  109. | {ok, term()}
  110. | {ok, term(), term()}
  111. | {ok, term(), term(), term()}
  112. | {error, {recoverable_error, term()}}
  113. | {error, term()}.
  114. -type batch_query_result() :: query_result() | [query_result()].
  115. -type action_resource_id() :: resource_id().
  116. -type source_resource_id() :: resource_id().
  117. -type connector_resource_id() :: resource_id().
  118. -type message_tag() :: action_resource_id().
  119. -define(WORKER_POOL_SIZE, 16).
  120. -define(DEFAULT_BUFFER_BYTES, 256 * 1024 * 1024).
  121. -define(DEFAULT_BUFFER_BYTES_RAW, <<"256MB">>).
  122. -define(DEFAULT_REQUEST_TTL, timer:seconds(45)).
  123. -define(DEFAULT_REQUEST_TTL_RAW, <<"45s">>).
  124. %% count
  125. -define(DEFAULT_BATCH_SIZE, 1).
  126. %% milliseconds
  127. -define(DEFAULT_BATCH_TIME, 0).
  128. -define(DEFAULT_BATCH_TIME_RAW, <<"0ms">>).
  129. %% count
  130. -define(DEFAULT_INFLIGHT, 100).
  131. %% milliseconds
  132. -define(HEALTHCHECK_INTERVAL, 15000).
  133. -define(HEALTHCHECK_INTERVAL_RAW, <<"15s">>).
  134. %% milliseconds
  135. -define(DEFAULT_METRICS_FLUSH_INTERVAL, 5_000).
  136. -define(DEFAULT_METRICS_FLUSH_INTERVAL_RAW, <<"5s">>).
  137. %% milliseconds
  138. -define(START_TIMEOUT, 5000).
  139. -define(START_TIMEOUT_RAW, <<"5s">>).
  140. %% boolean
  141. -define(START_AFTER_CREATED, true).
  142. %% Keep this test_id_prefix is match "^[A-Za-z0-9]+[A-Za-z0-9-_]*$".
  143. %% See `hocon_tconf`
  144. -define(TEST_ID_PREFIX, "t_probe_").
  145. -define(RES_METRICS, resource_metrics).
  146. -define(RESOURCE_ALLOCATION_TAB, emqx_resource_allocations).