-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy patheredis_cluster_pool_worker.erl
More file actions
67 lines (53 loc) · 1.69 KB
/
eredis_cluster_pool_worker.erl
File metadata and controls
67 lines (53 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-module(eredis_cluster_pool_worker).
-behaviour(gen_server).
-behaviour(poolboy_worker).
%% API.
-export([start_link/1]).
-export([query/2]).
%% gen_server.
-export([init/1]).
-export([handle_call/3]).
-export([handle_cast/2]).
-export([handle_info/2]).
-export([terminate/2]).
-export([code_change/3]).
-record(state, {conn}).
start_link(Args) ->
gen_server:start_link(?MODULE, Args, []).
init(Args) ->
Hostname = proplists:get_value(host, Args),
Port = proplists:get_value(port, Args),
DataBase = proplists:get_value(database, Args, 0),
Password = proplists:get_value(password, Args, ""),
process_flag(trap_exit, true),
Result = eredis:start_link(Hostname,Port, DataBase, Password),
process_flag(trap_exit, false),
Conn = case Result of
{ok,Connection} ->
Connection;
_ ->
undefined
end,
{ok, #state{conn=Conn}}.
query(full, _Commands) ->
{error, retry};
query(Worker, Commands) ->
gen_server:call(Worker, {'query', Commands}).
handle_call({'query', _}, _From, #state{conn = undefined} = State) ->
{reply, {error, no_connection}, State};
handle_call({'query', [[X|_]|_] = Commands}, _From, #state{conn = Conn} = State)
when is_list(X); is_binary(X) ->
{reply, eredis:qp(Conn, Commands), State};
handle_call({'query', Command}, _From, #state{conn = Conn} = State) ->
{reply, eredis:q(Conn, Command), State};
handle_call(_Request, _From, State) ->
{reply, ok, State}.
handle_cast(_Msg, State) ->
{noreply, State}.
handle_info(_Info, State) ->
{noreply, State}.
terminate(_Reason, #state{conn=Conn}) ->
ok = eredis:stop(Conn),
ok.
code_change(_OldVsn, State, _Extra) ->
{ok, State}.