Skip to content

Commit

Permalink
feat: expose redis username password config (#31)
Browse files Browse the repository at this point in the history
* feat: expose redis username password config

* feat: expose redis username password config for redis msg queues
  • Loading branch information
dhruvjain99 authored Nov 6, 2023
1 parent 3cde037 commit 92e2fa3
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ open(Opts) ->
SentinelHosts = vmq_schema_util:parse_list(proplists:get_value(host, Opts, "[\"localhost\"]")),
SentinelEndpoints = lists:foldr(fun(Host, Acc) -> [{Host, Port} | Acc]end, [], SentinelHosts),
ConnectOpts = [{sentinel, [{endpoints, SentinelEndpoints},
{username, Username},
{password, Password},
{timeout, proplists:get_value(connect_timeout, Opts, 5000)}]
},
{username, Username},
{password, Password},
{database, Database}],
eredis:start_link(ConnectOpts).

Expand Down
21 changes: 21 additions & 0 deletions apps/vmq_server/priv/vmq_server.schema
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
%% -*- mode: erlang -*-
%% ex: ft=erlang

%% @doc This option specifies the sentinel endpoints for subscription redis.
{mapping, "redis_sentinel_endpoints", "vmq_server.redis_sentinel_endpoints", [{default, "[{\"127.0.0.1\", 26379}]"}, {datatype, string}]}.

%% @doc This option specifies the subscription redis database number.
{mapping, "redis_sentinel_database", "vmq_server.redis_sentinel_database", [{default, 0}, {datatype, integer}]}.

%% @doc This option specifies the subscription redis username.
{mapping, "redis_sentinel_username", "vmq_server.redis_sentinel_username", [{datatype, string}]}.

%% @doc This option specifies the subscription redis password.
{mapping, "redis_sentinel_password", "vmq_server.redis_sentinel_password", [{datatype, string}]}.

%% @doc This option specifies the directory consisting of all the redis lua scripts used in vmq_server.
{mapping, "redis_lua_dir", "vmq_server.redis_lua_dir", [{default, "./etc/lua"}, {datatype, string}]}.

%% @doc This option specifies the time interval taken by the reaper process to reap the
Expand All @@ -26,6 +37,16 @@
"msg_queue_redis_shards_connect_options",
"vmq_server.msg_queue_redis_shards_connect_options", [{default, "[[{host,\"127.0.0.1\"},{port,6379},{database,1}]]"}, {datatype, string}]}.

%% @doc This option specifies the list of comma separated usernames of redis shards used for message passing.
{mapping,
"msg_queue_redis_shards_usernames",
"vmq_server.msg_queue_redis_shards_usernames", [{default, "[undefined]"}, {datatype, string}]}.

%% @doc This option specifies the list of comma separated passwords of redis shards used for message passing.
{mapping,
"msg_queue_redis_shards_passwords",
"vmq_server.msg_queue_redis_shards_passwords", [{default, "[undefined]"}, {datatype, string}]}.

%% @doc This option specifies the number of worker processes per redis shard
%% that will poll their main queues in message passing redis shard for new messages.
{mapping,
Expand Down
42 changes: 36 additions & 6 deletions apps/vmq_server/src/vmq_redis_queue_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,22 @@ init(_) ->
" ]]"
)
),
NumEndpoints = init_redis(ConnectOptionsList, 0),
UsernamesList = vmq_schema_util:parse_list(
application:get_env(
vmq_server,
msg_queue_redis_shards_usernames,
"[undefined]"
)
),
PasswordsList = vmq_schema_util:parse_list(
application:get_env(
vmq_server,
msg_queue_redis_shards_passwords,
"[undefined]"
)
),

NumEndpoints = init_redis(ConnectOptionsList, UsernamesList, PasswordsList, 0),

NumMainQWorkers = num_main_q_workers_per_redis_node(),
SupFlags =
Expand All @@ -60,13 +75,28 @@ init(_) ->
%%====================================================================
%% Internal functions
%%====================================================================
init_redis([], Id) ->
init_redis([], [], [], Id) ->
Id;
init_redis([ConnectOptions | ConnectOptionsList], Id) ->
init_redis(
[ConnectOptions | ConnectOptionsList],
[Username | UsernamesList],
[Password | PasswordsList],
Id
) ->
ProducerRedisClient = gen_redis_client_name(Id, ?PRODUCER),
ConsumerRedisClient = gen_redis_client_name(Id, ?CONSUMER),
{ok, _pid1} = eredis:start_link([{name, {local, ProducerRedisClient}} | ConnectOptions]),
{ok, _pid2} = eredis:start_link([{name, {local, ConsumerRedisClient}} | ConnectOptions]),
{ok, _pid1} = eredis:start_link([
{name, {local, ProducerRedisClient}},
{username, Username},
{password, Password}
| ConnectOptions
]),
{ok, _pid2} = eredis:start_link([
{name, {local, ConsumerRedisClient}},
{username, Username},
{password, Password}
| ConnectOptions
]),

LuaDir = application:get_env(vmq_server, redis_lua_dir, "./etc/lua"),
{ok, EnqueueMsgScript} = file:read_file(LuaDir ++ "/enqueue_msg.lua"),
Expand All @@ -79,7 +109,7 @@ init_redis([ConnectOptions | ConnectOptionsList], Id) ->
?FUNCTION, "LOAD", "REPLACE", PollMainQueueScript
]),

init_redis(ConnectOptionsList, Id + 1).
init_redis(ConnectOptionsList, UsernamesList, PasswordsList, Id + 1).

num_main_q_workers_per_redis_node() ->
application:get_env(vmq_server, main_queue_workers_per_redis_shard, 1).
Expand Down
4 changes: 4 additions & 0 deletions apps/vmq_server/src/vmq_server_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,17 @@ init([]) ->
application:get_env(vmq_server, redis_sentinel_endpoints, "[{\"127.0.0.1\", 26379}]")
),
RedisDB = application:get_env(vmq_server, redis_sentinel_database, 0),
Username = application:get_env(vmq_server, redis_sentinel_username, undefined),
Password = application:get_env(vmq_server, redis_sentinel_password, undefined),

{ok,
{{one_for_one, 5, 10}, [
?CHILD(eredis, worker, [
[
{sentinel, [{endpoints, SentinelEndpoints}]},
{database, RedisDB},
{username, Username},
{password, Password},
{name, {local, vmq_redis_client}}
]
]),
Expand Down

0 comments on commit 92e2fa3

Please sign in to comment.