Tony Garnock-Jones  committed ba4639c

Add linking connect variants, start_link/0 through /4.

  • Participants
  • Parent commits 01da05e

Comments (0)

Files changed (1)

File src/erldis_client.erl

 -export([scall/2, scall/3, scall/4, call/2, call/3, call/4, bcall/4, sr_scall/2, sr_scall/3]).
 -export([stop/1, transact/1, transact/2, select/2, info/1]).
 -export([connect/0, connect/1, connect/2, connect/3, connect/4]).
+-export([start_link/0, start_link/1, start_link/2, start_link/3, start_link/4]).
 -export([init/1, handle_call/3, handle_cast/2, handle_info/2,
 		 terminate/2, code_change/3]).
 -export([bin/1, format/2, format/1, sformat/1]).
 %% connect & init %%
-connect() ->
+connect() -> start(false).
+connect(Host) when is_list(Host) -> start(Host, false);
+connect(DB) when is_integer(DB) -> start(DB, false).
+connect(Host, Port) -> start(Host, Port, false).
+connect(Host, Port, Options) -> start(Host, Port, Options, false).
+connect(Host, Port, Options, DB) -> start(Host, Port, Options, DB, false).
+start_link() -> start(true).
+start_link(Host) when is_list(Host) -> start(Host, true);
+start_link(DB) when is_integer(DB) -> start(DB, true).
+start_link(Host, Port) -> start(Host, Port, true).
+start_link(Host, Port, Options) -> start(Host, Port, Options, true).
+start_link(Host, Port, Options, DB) -> start(Host, Port, Options, DB, true).
+start(ShouldLink) ->
 	{ok, Host} = app_get_env(erldis, host, "localhost"),
-	connect(Host).
+	start(Host, ShouldLink).
-connect(Host) when is_list(Host) ->
+start(Host, ShouldLink) when is_list(Host) ->
 	{ok, Port} = app_get_env(erldis, port, 6379),
-	connect(Host, Port);
-connect(DB) when is_integer(DB) ->
-	case connect() of
+	start(Host, Port, ShouldLink);
+start(DB, ShouldLink) when is_integer(DB) ->
+	case start(ShouldLink) of
 		{ok, Client} -> {ok, select(Client, DB)};
 		Other -> Other
-connect(Host, Port) ->
+start(Host, Port, ShouldLink) ->
 	{ok, Timeout} = app_get_env(erldis, timeout, 500),
-	connect(Host, Port, [{timeout, Timeout}]).
+	start(Host, Port, [{timeout, Timeout}], ShouldLink).
-connect(Host, Port, Options) ->
+start(Host, Port, Options, false) ->
 	% not using start_link because caller may not want to crash if this
 	% server is shutdown
-	gen_server2:start(?MODULE, [Host, Port], Options).
+	gen_server2:start(?MODULE, [Host, Port], Options);
+start(Host, Port, Options, true) ->
+	gen_server2:start_link(?MODULE, [Host, Port], Options).
-connect(Host, Port, Options, DB) ->
-	case connect(Host, Port, Options) of
+start(Host, Port, Options, DB, ShouldLink) ->
+	case start(Host, Port, Options, ShouldLink) of
 		{ok, Client} -> {ok, select(Client, DB)};
 		Other -> Other