emqx_resource.hrl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-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. -type resource_type() :: module().
  17. -type resource_id() :: binary().
  18. -type raw_resource_config() :: binary() | raw_term_resource_config().
  19. -type raw_term_resource_config() :: #{binary() => term()} | [raw_term_resource_config()].
  20. -type resource_config() :: term().
  21. -type resource_spec() :: map().
  22. -type resource_state() :: term().
  23. -type resource_status() :: connected | disconnected | connecting | stopped.
  24. -type callback_mode() :: always_sync | async_if_possible.
  25. -type query_mode() :: simple_sync | simple_async | sync | async | no_queries.
  26. -type result() :: term().
  27. -type reply_fun() :: {fun((result(), Args :: term()) -> any()), Args :: term()} | undefined.
  28. -type query_opts() :: #{
  29. %% The key used for picking a resource worker
  30. pick_key => term(),
  31. timeout => timeout(),
  32. expire_at => infinity | integer(),
  33. async_reply_fun => reply_fun(),
  34. simple_query => boolean(),
  35. is_buffer_supported => boolean(),
  36. reply_to => reply_fun()
  37. }.
  38. -type resource_data() :: #{
  39. id := resource_id(),
  40. mod := module(),
  41. callback_mode := callback_mode(),
  42. query_mode := query_mode(),
  43. config := resource_config(),
  44. error := term(),
  45. state := resource_state(),
  46. status := resource_status()
  47. }.
  48. -type resource_group() :: binary().
  49. -type creation_opts() :: #{
  50. %%======================================= Deprecated Opts BEGIN
  51. %% use health_check_interval instead
  52. health_check_timeout => integer(),
  53. %% use start_timeout instead
  54. wait_for_resource_ready => integer(),
  55. %% use health_check_interval instead
  56. auto_retry_interval => integer(),
  57. %% use health_check_interval instead
  58. auto_restart_interval => pos_integer() | infinity,
  59. %%======================================= Deprecated Opts END
  60. worker_pool_size => non_neg_integer(),
  61. %% use `integer()` compatibility to release 5.0.0 bpapi
  62. health_check_interval => integer(),
  63. %% We can choose to block the return of emqx_resource:start until
  64. %% the resource connected, wait max to `start_timeout` ms.
  65. start_timeout => pos_integer(),
  66. %% If `start_after_created` is set to true, the resource is started right
  67. %% after it is created. But note that a `started` resource is not guaranteed
  68. %% to be `connected`.
  69. start_after_created => boolean(),
  70. batch_size => pos_integer(),
  71. batch_time => pos_integer(),
  72. max_buffer_bytes => pos_integer(),
  73. query_mode => query_mode(),
  74. resume_interval => pos_integer(),
  75. inflight_window => pos_integer()
  76. }.
  77. -type query_result() ::
  78. ok
  79. | {ok, term()}
  80. | {ok, term(), term()}
  81. | {ok, term(), term(), term()}
  82. | {error, {recoverable_error, term()}}
  83. | {error, term()}.
  84. -define(WORKER_POOL_SIZE, 16).
  85. -define(DEFAULT_BUFFER_BYTES, 256 * 1024 * 1024).
  86. -define(DEFAULT_BUFFER_BYTES_RAW, <<"256MB">>).
  87. -define(DEFAULT_REQUEST_TTL, timer:seconds(45)).
  88. -define(DEFAULT_REQUEST_TTL_RAW, <<"45s">>).
  89. %% count
  90. -define(DEFAULT_BATCH_SIZE, 1).
  91. %% milliseconds
  92. -define(DEFAULT_BATCH_TIME, 0).
  93. -define(DEFAULT_BATCH_TIME_RAW, <<"0ms">>).
  94. %% count
  95. -define(DEFAULT_INFLIGHT, 100).
  96. %% milliseconds
  97. -define(HEALTHCHECK_INTERVAL, 15000).
  98. -define(HEALTHCHECK_INTERVAL_RAW, <<"15s">>).
  99. %% milliseconds
  100. -define(DEFAULT_METRICS_FLUSH_INTERVAL, 5_000).
  101. -define(DEFAULT_METRICS_FLUSH_INTERVAL_RAW, <<"5s">>).
  102. %% milliseconds
  103. -define(START_TIMEOUT, 5000).
  104. -define(START_TIMEOUT_RAW, <<"5s">>).
  105. %% boolean
  106. -define(START_AFTER_CREATED, true).
  107. -define(TEST_ID_PREFIX, "_probe_:").
  108. -define(RES_METRICS, resource_metrics).
  109. -define(RESOURCE_ALLOCATION_TAB, emqx_resource_allocations).