Source

erldis / src / erldis_client.erl

  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
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
%% @doc This is a	 very similar to erldis_client, but it does
%% synchronous calls instead of async pipelining. Does so by keeping a queue
%% of From pids in State.calls, then calls gen_server2:reply when it receives
%% handle_info({tcp, ...). Therefore, it must get commands on handle_call
%% instead of handle_cast, which requires direct command sending instead of
%% using the API in erldis.
%%
%% @todo Much of the code has been copied from erldis_client and should be
%% abstracted & shared where possible
%%
%% @author Jacob Perkins <japerk@gmail.com>
-module(erldis_client).

-behaviour(gen_server2).

-include("erldis.hrl").

-export([sr_scall/2, sr_scall/3, scall/2, scall/3, scall/4, call/2, call/3, call/4,
		 bcall/4, set_call/4, send/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]).
-define(EOL, "\r\n").

-define(default_timeout, 5000). %% same as in gen.erl in stdlib

%%%%%%%%%%%%%
%% helpers %%
%%%%%%%%%%%%%

bin(X) when is_list(X) ->
	list_to_binary(X);
bin(X) when is_atom(X) ->
	list_to_binary(atom_to_list(X));
bin(X) when is_binary(X) ->
	X;
bin(X) when is_integer(X) ->
	list_to_binary(integer_to_list(X));
bin(X) when is_float(X) ->
	list_to_binary(float_to_list(X));
bin(X) ->
	term_to_binary(X).

format(Lines) ->
	format(Lines, <<>>).

format([], Result) ->
	Result;
format([Line|Rest], <<>>) ->
	JoinedLine = binary_join(Line, <<" ">>),
	format(Rest, JoinedLine);
format([Line|Rest], Result) ->
	Sep = <<?EOL>>,
	JoinedLine = binary_join(Line, <<" ">>),
	format(Rest, <<Result/binary, Sep/binary, JoinedLine/binary>>).

sformat(<<>>)->
	<<>>;
sformat(Line) ->
	format([Line], <<>>).

binary_join([], _)->
	<<>>;
binary_join(Array, Sep)->
	F = fun(Elem, Acc) ->
			E2 = bin(Elem),
			<<Acc/binary, Sep/binary, E2/binary>>
		end,
	
	Sz = size(Sep),
	<<_:Sz/bytes, Result/binary>> = lists:foldl(F, <<>>, Array),
	Result.

trim2({ok, S}) ->
	Read = size(S)-2,
	<<R:Read/bytes, _/binary>> = S,
	R;
trim2(S) ->
	trim2({ok, S}).

app_get_env(AppName, Varname, Default) ->
	case application:get_env(AppName, Varname) of
		undefined ->
			{ok, Default};
		V ->
			V
	end.

%%%%%%%%%%%%%%%%%%%
%% call commands %%
%%%%%%%%%%%%%%%%%%%

select(Client, DB) ->
	DBB = list_to_binary(integer_to_list(DB)),
	[ok] = scall(Client, <<"select ", DBB/binary>>),
	Client.

sr_scall(Client, Cmd) -> sr_scall(Client, Cmd, []).

sr_scall(Client, Cmd, Args) ->
	case scall(Client, Cmd, Args) of
		[R] -> R;
		[] -> nil;
		ok -> ok
	end.

% This is the simple send with a single row of commands
scall(Client, Cmd) -> scall(Client, Cmd, <<>>, ?default_timeout).

scall(Client, Cmd, Args) -> scall(Client, Cmd, Args, ?default_timeout).

scall(Client, Cmd, Args, Timeout) ->
	Args2 = sformat(Args),
	send(Client, <<Cmd/binary, Args2/binary>>, Timeout).

% This is the complete send with multiple rows
call(Client, Cmd) -> call(Client, Cmd, [], ?default_timeout).

call(Client, Cmd, Args) -> call(Client, Cmd, Args, ?default_timeout).

call(Client, Cmd, Args, Timeout) ->
	Args2 = format(Args),
	send(Client, <<Cmd/binary, Args2/binary>>, Timeout).

% Blocking call with server-side timeout added as final command arg
bcall(Client, Cmd, Args, Timeout) ->
    scall(Client, Cmd, Args ++ [server_timeout(Timeout)], erlang_timeout(Timeout)).

set_call(Client, Cmd, Key, Val) when is_binary(Val) ->
	call(Client, Cmd, [[Key, erlang:size(Val)], [Val]]);
set_call(Client, Cmd, Key, Val) ->
	set_call(Client, Cmd, Key, bin(Val)).

% Erlang uses milliseconds, with symbol "infinity" for "wait forever";
% redis uses seconds, with 0 for "wait forever".
server_timeout(infinity) -> 0;
server_timeout(V) when is_number(V) -> V / 1000.

% Kludge on a few milliseconds to the timeout we gave the server, to
% give the network and client a chance to catch up.
erlang_timeout(infinity) -> infinity;
erlang_timeout(V) when is_number(V) -> V + ?default_timeout.

send(Client, Cmd, Timeout) ->
	Piped = gen_server2:call(Client, is_pipelined),
	
	if
		Piped ->
			gen_server2:cast(Client, {send, Cmd});
		true ->
			case gen_server2:call(Client, {send, Cmd}, Timeout) of
				{error, Reason} -> throw({error, Reason});
				Retval -> Retval
			end
	end.

% TODO: use multi/exec for transact (and move transact to erldis.erl)
transact(F) ->
	case connect() of
		{error, Error} -> {error, Error};
		{ok, Client} -> transact(Client, F)
	end.

transact(DB, F) when is_integer(DB) ->
	case connect(DB) of
		{error, Error} -> {error, Error};
		{ok, Client} -> transact(Client, F)
	end;
transact(Client, F) when is_pid(Client) ->
	try F(Client) of
		Result -> stop(Client), Result
	catch
		throw:Result -> stop(Client), throw(Result);
		error:Result -> stop(Client), {error, Result};
		exit:Result -> stop(Client), exit(Result)
	end.

%%%%%%%%%%%%%%%%
%% redis info %%
%%%%%%%%%%%%%%%%

info(Client) ->
	F = fun(Stat) ->
			case parse_stat(Stat) of
				undefined -> false;
				{Key, Val} -> {Key, Val}
			end
		end,

	[S] = scall(Client, <<"info ">>),
	elists:mapfilter(F, string:tokens(binary_to_list(S), ?EOL)).

parse_stat("redis_version:"++Vsn) ->
	{version, Vsn};
parse_stat("uptime_in_seconds:"++Val) ->
	{uptime, list_to_integer(Val)};
parse_stat("connected_clients:"++Val) ->
	{clients, list_to_integer(Val)};
parse_stat("connected_slaves:"++Val) ->
	{slaves, list_to_integer(Val)};
parse_stat("used_memory:"++Val) ->
	{memory, list_to_integer(Val)};
parse_stat("changes_since_last_save:"++Val) ->
	{changes, list_to_integer(Val)};
parse_stat("last_save_time:"++Val) ->
	{last_save, list_to_integer(Val)};
parse_stat("total_connections_received:"++Val) ->
	{connections, list_to_integer(Val)};
parse_stat("total_commands_processed:"++Val) ->
	{commands, list_to_integer(Val)};
parse_stat(_) ->
	undefined.

%%%%%%%%%%%%%%%%%%%%
%% connect & init %%
%%%%%%%%%%%%%%%%%%%%

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"),
	start(Host, ShouldLink).

start(Host, ShouldLink) when is_list(Host) ->
	{ok, Port} = app_get_env(erldis, port, 6379),
	start(Host, Port, ShouldLink);
start(DB, ShouldLink) when is_integer(DB) ->
	case start(ShouldLink) of
		{ok, Client} -> {ok, select(Client, DB)};
		Other -> Other
	end.

start(Host, Port, ShouldLink) ->
	{ok, Timeout} = app_get_env(erldis, timeout, 500),
	start(Host, Port, [{timeout, Timeout}], ShouldLink).

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);
start(Host, Port, Options, true) ->
	gen_server2:start_link(?MODULE, [Host, Port], Options).

start(Host, Port, Options, DB, ShouldLink) ->
	case start(Host, Port, Options, ShouldLink) of
		{ok, Client} -> {ok, select(Client, DB)};
		Other -> Other
	end.

% stop is synchronous so can be sure that client is shutdown
stop(Client) -> gen_server2:call(Client, disconnect).

init([Host, Port]) ->
	process_flag(trap_exit, true),
	{ok, Timeout} = app_get_env(erldis, timeout, 500),
	State = #redis{calls=queue:new(), host=Host, port=Port, timeout=Timeout},
	
	case connect_socket(State, once) of
		{error, Why} -> {stop, {socket_error, Why}};
		{ok, NewState} -> {ok, NewState}
	end.

ensure_started(#redis{socket=undefined, db=DB}=State) ->
	case connect_socket(State, false) of
		{error, Why} ->
			Report = [{?MODULE, unable_to_connect}, {error, Why}, State],
			error_logger:warning_report(Report),
			State;
		{ok, NewState} ->
			Socket = NewState#redis.socket,
			
			if
				DB == <<"0">> ->
					ok;
				true ->
					gen_tcp:send(Socket, <<"select ", DB/binary, ?EOL>>),
					{ok, <<"+OK", _R/binary>>} = gen_tcp:recv(Socket, 10)
			end,
			
			inet:setopts(Socket, [{active, once}]),
			NewState
	end;
ensure_started(State)->
	State.

connect_socket(#redis{socket=undefined, host=Host, port=Port, timeout=Timeout}=State, Active) ->
	% NOTE: send_timeout_close not used because causes {error, badarg}
	Opts = [binary, {active, Active}, {packet, line}, {nodelay, true},
			{send_timeout, Timeout}],
	% without timeout, default is infinity
	case gen_tcp:connect(Host, Port, Opts, Timeout) of
		{ok, Socket} -> {ok, State#redis{socket=Socket}};
		{error, Why} -> {error, Why}
	end;
connect_socket(State, _) ->
	{ok, State}.

%%%%%%%%%%%%%%%%%
%% handle_call %%
%%%%%%%%%%%%%%%%%

handle_call(is_pipelined, _From, State)->
  {reply, State#redis.pipeline, State};
handle_call(get_all_results, From, #redis{pipeline=true, calls=Calls} = State) ->
	case queue:len(Calls) of
		0 ->
			% answers came earlier than we could start listening...
			% Very unlikely but totally possible.
			Reply = lists:reverse(State#redis.results),
			{reply, Reply, State#redis{results=[], calls=Calls}};
		_ ->
			% We are here earlier than results came, so just make
			% ourselves wait until stuff is ready.
			R = fun(V) -> gen_server2:reply(From, V) end,
			{noreply, State#redis{reply_caller=R}}
	end;
handle_call({send, Cmd}, From, State1) ->
	% NOTE: redis ignores sent commands it doesn't understand, which means
	% we don't get a reply, which means callers will timeout
	State = ensure_started(State1),
	
	case gen_tcp:send(State#redis.socket, [Cmd | <<?EOL>>]) of
		ok ->
			Queue = queue:in(From, State#redis.calls),
			
			case Cmd of
				<<"select ", DB/binary>> ->
					{noreply, State#redis{calls=Queue, remaining=1, db=DB}};
				_ ->
					{noreply, State#redis{calls=Queue, remaining=1}}
			end;
		{error, Reason} ->
			error_logger:error_report([{send, Cmd}, {error, Reason}]),
			{stop, timeout, {error, Reason}, State}
	end;
handle_call(disconnect, _, State) ->
	{stop, shutdown, shutdown, State};
handle_call(_, _, State) ->
	{reply, undefined, State}.

%%%%%%%%%%%%%%%%%
%% handle_cast %%
%%%%%%%%%%%%%%%%%

handle_cast({pipelining, Bool}, State) ->
	{noreply, State#redis{pipeline=Bool}};
handle_cast(disconnect, State) ->
	{stop, shutdown, State};
handle_cast({send, Cmd}, #redis{remaining=Remaining, calls=Calls} = State1) ->
	End = <<?EOL>>,
	State = ensure_started(State1),
	Queue = queue:in(async, Calls),
	gen_tcp:send(State#redis.socket, [Cmd|End]),
	
	case Remaining of
		0 -> {noreply, State#redis{remaining=1, calls=Queue}};
		_ -> {noreply, State#redis{calls=Queue}}
	end;
handle_cast(_, State) ->
	{noreply, State}.

%%%%%%%%%%%%%%%%%
%% handle_info %%
%%%%%%%%%%%%%%%%%

recv_value(Socket, NBytes) ->
	inet:setopts(Socket, [{packet, 0}]), % go into raw mode to read bytes
	
	case gen_tcp:recv(Socket, NBytes+2) of
		{ok, Packet} ->
			inet:setopts(Socket, [{packet, line}]), % go back to line mode
			trim2({ok, Packet});
		{error, Reason} ->
			error_logger:error_report([{recv, NBytes}, {error, Reason}]),
			throw({error, Reason})
	end.

send_reply(#redis{pipeline=true, calls=Calls, results=Results, reply_caller=ReplyCaller}=State)->
	Result = case lists:reverse(State#redis.buffer) of
		[V] when is_atom(V) -> V;
		R -> R
	end,
	
	{_, Queue} = queue:out(Calls),
	
	case queue:len(Queue) of
		0 ->
			FullResults = [Result|Results],
			
			NewState = case ReplyCaller of
				undefined ->
					State#redis{results=FullResults};
				_ ->
					ReplyCaller(lists:reverse(FullResults)),
					State#redis{results=[]}
			end,
			
			NewState#redis{remaining=0, pstate=empty,
						 reply_caller=undefined, buffer=[],
						 calls=Queue};
		_ ->
			State#redis{results=[Result|Results], remaining=1, pstate=empty, buffer=[], calls=Queue}
	end;

send_reply(State) ->
	case queue:out(State#redis.calls) of
		{{value, From}, Queue} ->
			Reply = lists:reverse(State#redis.buffer),
			gen_server2:reply(From, Reply);
		{empty, Queue} ->
			ok
	end,
	
	State#redis{calls=Queue, buffer=[], pstate=empty}.

parse_state(State, Socket, Data) ->
	Parse = erldis_proto:parse(State#redis.pstate, trim2(Data)),
	
	case {State#redis.remaining-1, Parse} of
		{0, error} ->
			% next line is the error string
			State#redis{remaining=1, pstate=error};
		{0, {hold, nil}} ->
			% reply with values in buffer
			send_reply(State);
		{0, {hold, Remaining}} ->
			% begin accumulation of multi bulk reply
			State#redis{remaining=Remaining, pstate=read};
		{_, {read, nil}} ->
			% reply with nil
			send_reply(State#redis{buffer=[nil]});
		{_, {read, 0}} ->
			% reply with nil
			send_reply(State#redis{buffer=[]});
		{0, {read, NBytes}} ->
			% reply with Value added to buffer
			Value = recv_value(Socket, NBytes),
			Buffer = [Value | State#redis.buffer],
			send_reply(State#redis{buffer=Buffer});
		{N, {read, NBytes}} ->
			% accumulate multi bulk reply
			Value = recv_value(Socket, NBytes),
			Buffer = [Value | State#redis.buffer],
			State#redis{remaining=N, buffer=Buffer, pstate=read};
		{0, Value} ->
			% reply with Value
			Buffer = [Value | State#redis.buffer],
			send_reply(State#redis{buffer=Buffer});
		{N, Value} ->
			Buffer = [Value | State#redis.buffer],
			State#redis{remaining=N, buffer=Buffer, pstate=read}
	end.

handle_info({tcp, Socket, Data}, State) ->
	case (catch parse_state(State, Socket, Data)) of
		{error, Reason} ->
			{stop, Reason, State};
		NewState ->
			inet:setopts(Socket, [{active, once}]),
			{noreply, NewState}
	end;
handle_info({tcp_closed, Socket}, State=#redis{socket=Socket}) ->
	{noreply, State#redis{socket=undefined}};
handle_info(_Info, State) ->
	{noreply, State}.

%%%%%%%%%%%%%%%
%% terminate %%
%%%%%%%%%%%%%%%

terminate(_Reason, State) ->
	% NOTE: if supervised with brutal_kill, may not be able to reply
	R = fun(From) -> gen_server2:reply(From, {error, closed}) end,
	lists:foreach(R, queue:to_list(State#redis.calls)),
	
	case State#redis.socket of
		undefined -> ok;
		Socket -> gen_tcp:close(Socket)
	end.

code_change(_OldVsn, State, _Extra) -> {ok, State}.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.