Permalink
Browse files

fix some typo in README.md; add some auxiliary functions in conn_sup

  • Loading branch information...
1 parent e0a05b5 commit 162621bf8121dd1ecb296e2c5e19fa0bc7a294ca @litaocheng committed Jul 15, 2010
Showing with 63 additions and 9 deletions.
  1. +21 −5 README.md
  2. +2 −2 include/redis_internal.hrl
  3. +39 −1 src/redis_conn_sup.erl
  4. +1 −1 vsn.mk
View
26 README.md
@@ -65,25 +65,25 @@ e.g. 2 (with registered name)
e.g. 3 (use the redis in OTP)
% in main supervisor:
- {redis_client_sup, {redis_conn_sup, start_link, []},
+ {redis_conn_sup, {redis_conn_sup, start_link, []},
permanent, 1000, supervisor, [redis_client]}
% start a redis client:
Name = redis_client:name(Host, Port),
- {ok, _} = redis_cilent_sup:connect(Host, Port, Pass, Name),
+ {ok, _} = redis_conn_sup:connect(Host, Port, Pass, Name),
Redis = redis_client:handler(Name),
Redis:set("k1", "v1"),
Redis:get("k1").
e.g. 4 (use in OTP with connection pool)
% in main supervisor:
- {redis_client_sup, {redis_conn_sup, start_link, []},
+ {redis_conn_sup, {redis_conn_sup, start_link, []},
permanent, 1000, supervisor, [redis_client]}
% start client pool
[begin
- Name = redis_client:name(Host, Port, I)
- {ok, _} = redis_cilent_sup:connect(Host, Port, Pass, Name)
+ Name = redis_client:name(Host, Port, I),
+ {ok, _} = redis_conn_sup:connect(Host, Port, Pass, Name)
end || I <- lists:seq(1, 5)],
% random select a client
@@ -92,6 +92,22 @@ e.g. 4 (use in OTP with connection pool)
Redis:set("k1", "v1"),
Redis:get("k1").
+e.g.5 (use in OTP by the auxiliary functions)
+ % in main supervisor:
+ [redis_conn_sup:sup_spec()]
+
+ % start the client pool in the main supervisor init functions
+ {ok, Pid} = superviosr:start_link ....
+ redis_conn_sup:sup_start_client(Host, Port, Pass, Pool),
+ {ok, Pid}.
+
+ % in the code, random select one client
+ Redis = redis_conn_sup:sup_rand_client(Host, Port, Pass, Pool),
+ % or
+ % Redis = redis_conn_sup:sup_rand_client(),
+ Redis:set("k1", "v1"),
+ Redis:get("k1").
+
## Benchmark
in my laptop,
CPU: Intel(R) Core(TM)2 Duo CPU T5870)
View
4 include/redis_internal.hrl
@@ -49,12 +49,12 @@
end).
%% the redis supervisor name
--define(REDIS_SUP, redis_sup).
-define(CONN_SUP, redis_conn_sup).
-define(CONN_TIMEOUT, 5000).
-define(RECV_TIMEOUT, 2000).
+-define(CONN_POOL_DEF, 5).
-define(CONN_POOL_MIN, 1).
--define(CONN_POOL_MAX, 32).
+-define(CONN_POOL_MAX, 64).
%%
%% about types and records
View
40 src/redis_conn_sup.erl
@@ -16,8 +16,14 @@
-export([start_link/0]).
-export([connect/3, connect/4]).
--export([init/1]).
+%% some auxiliary functions
+-export([sup_spec/0
+ , sup_start_client/3, sup_start_client/4
+ , sup_rand_client/0, sup_rand_client/2, sup_rand_client/3]).
+
+%% supervisor callback
+-export([init/1]).
%% @doc the application start callback
-spec start_link() ->
@@ -34,6 +40,38 @@ connect(Host, Port, Passwd) ->
connect(Host, Port, Passwd, Name) ->
supervisor:start_child(?CONN_SUP, [Host, Port, Passwd, Name]).
+%% @doc return the specification as the child of other supervisor
+sup_spec() ->
+ {redis_client_sup, {redis_conn_sup, start_link, []},
+ permanent, 1000, supervisor, [redis_client]}.
+
+
+%% @doc start client to Host:Port with a connection pool, in the ?CONN_SUP
+sup_start_client(Host, Port, Pass) ->
+ sup_start_client(Host, Port, Pass, ?CONN_POOL_DEF).
+
+sup_start_client(Host, Port, Pass, Pool) when is_integer(Pool) ->
+ [begin
+ Name = redis_client:name(Host, Port, I),
+ {ok, _} = connect(Host, Port, Pass, Name)
+ end || I <- lists:seq(1, Pool)].
+
+%% @doc random select one connection form the ?CONN_SUP
+sup_rand_client() ->
+ Children = supervisor:which_children(?CONN_SUP),
+ Len = length(Children),
+ {_Id, Child, worker, _Modules} = lists:nth(random:uniform(Len), Children),
+ redis_client:handler(Child).
+
+%% @doc random select one connection with Host:Port from the connection pool
+%% return the parameterized redis module
+sup_rand_client(Host, Port) ->
+ sup_rand_client(Host, Port, ?CONN_POOL_DEF).
+
+sup_rand_client(Host, Port, Pool) ->
+ Selected = redis_client:existing_name(Host, Port, random:uniform(Pool)),
+ redis_client:handler(Selected).
+
%% @doc the connection supervisor callback
init([]) ->
?DEBUG2("init supervisor", []),
View
2 vsn.mk
@@ -1,2 +1,2 @@
APP_NAME=redis
-APP_VSN=1.0.0
+APP_VSN=1.0.1

0 comments on commit 162621b

Please sign in to comment.