refactor emqttd_conf

This commit is contained in:
Feng 2016-09-09 22:52:17 +08:00
parent 61d8b2d907
commit 7d9ad96021
3 changed files with 33 additions and 27 deletions

View File

@ -53,7 +53,7 @@
-define(APP, ?MODULE). -define(APP, ?MODULE).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Bootstrap, environment, is_running... %% Bootstrap, environment, configuration, is_running...
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% @doc Start emqttd application. %% @doc Start emqttd application.
@ -62,10 +62,10 @@ start() -> application:start(?APP).
%% @doc Get Config %% @doc Get Config
-spec(conf(Key :: atom()) -> any()). -spec(conf(Key :: atom()) -> any()).
conf(Key) -> gen_conf:value(?APP, Key). conf(Key) -> emqttd_conf:value(Key).
-spec(conf(Key :: atom(), Default :: any()) -> any()). -spec(conf(Key :: atom(), Default :: any()) -> any()).
conf(Key, Default) -> gen_conf:value(?APP, Key, Default). conf(Key, Default) -> emqttd_conf:value(Key, Default).
%% @doc Environment %% @doc Environment
-spec(env(Key:: atom()) -> any()). -spec(env(Key:: atom()) -> any()).

View File

@ -42,7 +42,7 @@
Reason :: term()). Reason :: term()).
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
print_banner(), print_banner(),
gen_conf:init(emqttd), emqttd_conf:init(),
emqttd_mnesia:start(), emqttd_mnesia:start(),
{ok, Sup} = emqttd_sup:start_link(), {ok, Sup} = emqttd_sup:start_link(),
start_servers(Sup), start_servers(Sup),

View File

@ -16,83 +16,90 @@
-module(emqttd_conf). -module(emqttd_conf).
-import(gen_conf, [value/3]). -export([init/0]).
-export([mqtt/0, session/0, queue/0, bridge/0, pubsub/0]). -export([mqtt/0, session/0, queue/0, bridge/0, pubsub/0]).
-export([list/1]). -export([value/1, value/2, list/1]).
-define(APP, emqttd). -define(APP, emqttd).
init() -> gen_conf:init(?APP).
mqtt() -> mqtt() ->
with_env(mqtt_protocol, [ with_env(mqtt_protocol, [
%% Max ClientId Length Allowed. %% Max ClientId Length Allowed.
{max_clientid_len, value(?APP, mqtt_max_clientid_len, 512)}, {max_clientid_len, value(mqtt_max_clientid_len, 512)},
%% Max Packet Size Allowed, 64K by default. %% Max Packet Size Allowed, 64K by default.
{max_packet_size, value(?APP, mqtt_max_packet_size, 65536)}, {max_packet_size, value(mqtt_max_packet_size, 65536)},
%% Client Idle Timeout. %% Client Idle Timeout.
{client_idle_timeout, value(?APP, mqtt_client_idle_timeout, 30)} {client_idle_timeout, value(mqtt_client_idle_timeout, 30)}
]). ]).
session() -> session() ->
with_env(mqtt_session, [ with_env(mqtt_session, [
%% Max number of QoS 1 and 2 messages that can be inflight at one time. %% Max number of QoS 1 and 2 messages that can be inflight at one time.
%% 0 means no limit %% 0 means no limit
{max_inflight, value(?APP, session_max_inflight, 100)}, {max_inflight, value(session_max_inflight, 100)},
%% Retry interval for redelivering QoS1/2 messages. %% Retry interval for redelivering QoS1/2 messages.
{unack_retry_interval, value(?APP, session_unack_retry_interval, 60)}, {unack_retry_interval, value(session_unack_retry_interval, 60)},
%% Awaiting PUBREL Timeout %% Awaiting PUBREL Timeout
{await_rel_timeout, value(?APP, session_await_rel_timeout, 20)}, {await_rel_timeout, value(session_await_rel_timeout, 20)},
%% Max Packets that Awaiting PUBREL, 0 means no limit %% Max Packets that Awaiting PUBREL, 0 means no limit
{max_awaiting_rel, value(?APP, session_max_awaiting_rel, 0)}, {max_awaiting_rel, value(session_max_awaiting_rel, 0)},
%% Statistics Collection Interval(seconds) %% Statistics Collection Interval(seconds)
{collect_interval, value(?APP, session_collect_interval, 0)}, {collect_interval, value(session_collect_interval, 0)},
%% Expired after 2 day (unit: minute) %% Expired after 2 day (unit: minute)
{expired_after, value(?APP, session_expired_after, 2880)} {expired_after, value(session_expired_after, 2880)}
]). ]).
queue() -> queue() ->
with_env(mqtt_queue, [ with_env(mqtt_queue, [
%% Type: simple | priority %% Type: simple | priority
{type, value(?APP, queue_type, simple)}, {type, value(queue_type, simple)},
%% Topic Priority: 0~255, Default is 0 %% Topic Priority: 0~255, Default is 0
{priority, value(?APP, queue_priority, [])}, {priority, value(queue_priority, [])},
%% Max queue length. Enqueued messages when persistent client disconnected, %% Max queue length. Enqueued messages when persistent client disconnected,
%% or inflight window is full. %% or inflight window is full.
{max_length, value(?APP, queue_max_length, infinity)}, {max_length, value(queue_max_length, infinity)},
%% Low-water mark of queued messages %% Low-water mark of queued messages
{low_watermark, value(?APP, queue_low_watermark, 0.2)}, {low_watermark, value(queue_low_watermark, 0.2)},
%% High-water mark of queued messages %% High-water mark of queued messages
{high_watermark, value(?APP, queue_high_watermark, 0.6)}, {high_watermark, value(queue_high_watermark, 0.6)},
%% Queue Qos0 messages? %% Queue Qos0 messages?
{queue_qos0, value(?APP, queue_qos0, true)} {queue_qos0, value(queue_qos0, true)}
]). ]).
bridge() -> bridge() ->
with_env(mqtt_bridge, [ with_env(mqtt_bridge, [
%% TODO: Bridge Queue Size {max_queue_len, value(bridge_max_queue_len, 10000)},
{max_queue_len, value(?APP, bridge_max_queue_len, 10000)},
%% Ping Interval of bridge node %% Ping Interval of bridge node
{ping_down_interval, value(?APP, bridge_ping_down_interval, 1)} {ping_down_interval, value(bridge_ping_down_interval, 1)}
]). ]).
pubsub() -> pubsub() ->
with_env(mqtt_pubsub, [ with_env(mqtt_pubsub, [
%% PubSub and Router. Default should be scheduler numbers. %% PubSub and Router. Default should be scheduler numbers.
{pool_size, value(?APP, pubsub_pool_size, 8)} {pool_size, value(pubsub_pool_size, 8)}
]). ]).
value(Key) ->
with_env(Key, gen_conf:value(?APP, Key)).
value(Key, Default) ->
with_env(Key, gen_conf:value(?APP, Key, Default)).
with_env(Key, Conf) -> with_env(Key, Conf) ->
case application:get_env(?APP, Key) of case application:get_env(?APP, Key) of
undefined -> undefined ->
@ -101,6 +108,5 @@ with_env(Key, Conf) ->
Val Val
end. end.
list(Key) -> list(Key) -> gen_conf:list(?APP, Key).
gen_conf:list(?APP, Key).