24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-10 21:47:01 +02:00

Improve type specs and return values

This commit is contained in:
Evgeniy Khramtsov 2017-04-06 20:56:46 +03:00
parent 6876a37e61
commit 245fe04289

View File

@ -55,7 +55,11 @@
num :: pos_integer(), num :: pos_integer(),
pending_q :: p1_queue:queue()}). pending_q :: p1_queue:queue()}).
-type redis_error() :: {error, binary() | atom()}. -type redis_error() :: {error, binary() | timeout | disconnected}.
-type redis_reply() :: binary() | [binary()].
-type redis_command() :: [binary()].
-type redis_pipeline() :: [redis_command()].
-type state() :: #state{}.
%%%=================================================================== %%%===================================================================
%%% API %%% API
@ -73,13 +77,15 @@ get_connection(I) ->
iolist_to_binary( iolist_to_binary(
[atom_to_list(?MODULE), "_connection_", integer_to_list(I)])). [atom_to_list(?MODULE), "_connection_", integer_to_list(I)])).
-spec q(redis_command()) -> {ok, redis_reply()} | redis_error().
q(Command) -> q(Command) ->
call(get_worker(), {q, Command}, ?MAX_RETRIES). call(get_worker(), {q, Command}, ?MAX_RETRIES).
-spec qp(redis_pipeline()) -> {ok, [redis_reply()]} | redis_error().
qp(Pipeline) -> qp(Pipeline) ->
call(get_worker(), {qp, Pipeline}, ?MAX_RETRIES). call(get_worker(), {qp, Pipeline}, ?MAX_RETRIES).
-spec multi(fun(() -> any())) -> {ok, list()} | redis_error(). -spec multi(fun(() -> any())) -> {ok, [redis_reply()]} | redis_error().
multi(F) -> multi(F) ->
case erlang:get(?TR_STACK) of case erlang:get(?TR_STACK) of
undefined -> undefined ->
@ -98,7 +104,7 @@ multi(F) ->
erlang:raise(E, R, erlang:get_stacktrace()) erlang:raise(E, R, erlang:get_stacktrace())
end; end;
_ -> _ ->
{error, nested_transaction} erlang:error(nested_transaction)
end. end.
-spec get(iodata()) -> {ok, undefined | binary()} | redis_error(). -spec get(iodata()) -> {ok, undefined | binary()} | redis_error().
@ -107,7 +113,7 @@ get(Key) ->
undefined -> undefined ->
q([<<"GET">>, Key]); q([<<"GET">>, Key]);
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec set(iodata(), iodata()) -> ok | redis_error() | queued. -spec set(iodata(), iodata()) -> ok | redis_error() | queued.
@ -174,7 +180,7 @@ smembers(Set) ->
undefined -> undefined ->
q([<<"SMEMBERS">>, Set]); q([<<"SMEMBERS">>, Set]);
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec sismember(iodata(), iodata()) -> boolean() | redis_error(). -spec sismember(iodata(), iodata()) -> boolean() | redis_error().
@ -186,7 +192,7 @@ sismember(Set, Member) ->
{error, _} = Err -> Err {error, _} = Err -> Err
end; end;
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec scard(iodata()) -> {ok, non_neg_integer()} | redis_error(). -spec scard(iodata()) -> {ok, non_neg_integer()} | redis_error().
@ -200,7 +206,7 @@ scard(Set) ->
Err Err
end; end;
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec hget(iodata(), iodata()) -> {ok, undefined | binary()} | redis_error(). -spec hget(iodata(), iodata()) -> {ok, undefined | binary()} | redis_error().
@ -209,7 +215,7 @@ hget(Key, Field) ->
undefined -> undefined ->
q([<<"HGET">>, Key, Field]); q([<<"HGET">>, Key, Field]);
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec hset(iodata(), iodata(), iodata()) -> {ok, boolean()} | redis_error() | queued. -spec hset(iodata(), iodata(), iodata()) -> {ok, boolean()} | redis_error() | queued.
@ -249,7 +255,7 @@ hgetall(Key) ->
{error, _} = Err -> Err {error, _} = Err -> Err
end; end;
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec hlen(iodata()) -> {ok, non_neg_integer()} | redis_error(). -spec hlen(iodata()) -> {ok, non_neg_integer()} | redis_error().
@ -261,7 +267,7 @@ hlen(Key) ->
{error, _} = Err -> Err {error, _} = Err -> Err
end; end;
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
-spec hkeys(iodata()) -> {ok, [binary()]} | redis_error(). -spec hkeys(iodata()) -> {ok, [binary()]} | redis_error().
@ -270,7 +276,7 @@ hkeys(Key) ->
undefined -> undefined ->
q([<<"HKEYS">>, Key]); q([<<"HKEYS">>, Key]);
_ -> _ ->
{error, transaction_unsupported} erlang:error(transaction_unsupported)
end. end.
%%%=================================================================== %%%===================================================================
@ -340,6 +346,7 @@ code_change(_OldVsn, State, _Extra) ->
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
-spec connect(state()) -> {ok, pid()} | {error, any()}.
connect(#state{num = Num}) -> connect(#state{num = Num}) ->
Server = ejabberd_config:get_option(redis_server, Server = ejabberd_config:get_option(redis_server,
fun iolist_to_list/1, fun iolist_to_list/1,
@ -381,9 +388,12 @@ connect(#state{num = Num}) ->
{error, Reason} {error, Reason}
end. end.
-spec call({atom(), atom()}, {q | qp, list()}, integer()) -> -spec call({atom(), atom()}, {q, redis_command()}, integer()) ->
{error, disconnected | timeout | binary()} | {ok, iodata()}. {ok, redis_reply()} | redis_error();
({atom(), atom()}, {qp, redis_pipeline()}, integer()) ->
{ok, [redis_reply()]} | redis_error().
call({Conn, Parent}, {F, Cmd}, Retries) -> call({Conn, Parent}, {F, Cmd}, Retries) ->
?DEBUG("redis query: ~p", [Cmd]),
Res = try eredis:F(Conn, Cmd, ?CALL_TIMEOUT) of Res = try eredis:F(Conn, Cmd, ?CALL_TIMEOUT) of
{error, Reason} when is_atom(Reason) -> {error, Reason} when is_atom(Reason) ->
try exit(whereis(Conn), kill) catch _:_ -> ok end, try exit(whereis(Conn), kill) catch _:_ -> ok end,
@ -405,11 +415,14 @@ call({Conn, Parent}, {F, Cmd}, Retries) ->
Res Res
end. end.
-spec get_worker() -> {atom(), atom()}.
get_worker() -> get_worker() ->
Time = p1_time_compat:system_time(), Time = p1_time_compat:system_time(),
I = erlang:phash2(Time, ejabberd_redis_sup:get_pool_size()) + 1, I = erlang:phash2(Time, ejabberd_redis_sup:get_pool_size()) + 1,
{get_connection(I), get_proc(I)}. {get_connection(I), get_proc(I)}.
-spec get_result([{error, atom() | binary()} | {ok, iodata()}]) ->
{ok, [redis_reply()]} | {error, binary()}.
get_result([{error, _} = Err|_]) -> get_result([{error, _} = Err|_]) ->
Err; Err;
get_result([{ok, _} = OK]) -> get_result([{ok, _} = OK]) ->
@ -422,9 +435,11 @@ tr_enq(Cmd, Stack) ->
erlang:put(?TR_STACK, [Cmd|Stack]), erlang:put(?TR_STACK, [Cmd|Stack]),
queued. queued.
-spec decode_pairs([binary()]) -> [{binary(), binary()}].
decode_pairs(Pairs) -> decode_pairs(Pairs) ->
decode_pairs(Pairs, []). decode_pairs(Pairs, []).
-spec decode_pairs([binary()], [{binary(), binary()}]) -> [{binary(), binary()}].
decode_pairs([Field, Val|Pairs], Acc) -> decode_pairs([Field, Val|Pairs], Acc) ->
decode_pairs(Pairs, [{Field, Val}|Acc]); decode_pairs(Pairs, [{Field, Val}|Acc]);
decode_pairs([], Acc) -> decode_pairs([], Acc) ->
@ -433,15 +448,18 @@ decode_pairs([], Acc) ->
dec_bool(<<$1>>) -> true; dec_bool(<<$1>>) -> true;
dec_bool(<<$0>>) -> false. dec_bool(<<$0>>) -> false.
-spec reply(T) -> {ok, T} | queued.
reply(Val) -> reply(Val) ->
case erlang:get(?TR_STACK) of case erlang:get(?TR_STACK) of
undefined -> {ok, Val}; undefined -> {ok, Val};
_ -> queued _ -> queued
end. end.
-spec iolist_to_list(iodata()) -> string().
iolist_to_list(IOList) -> iolist_to_list(IOList) ->
binary_to_list(iolist_to_binary(IOList)). binary_to_list(iolist_to_binary(IOList)).
-spec max_fsm_queue() -> pos_integer().
max_fsm_queue() -> max_fsm_queue() ->
proplists:get_value(max_queue, fsm_limit_opts(), ?DEFAULT_MAX_QUEUE). proplists:get_value(max_queue, fsm_limit_opts(), ?DEFAULT_MAX_QUEUE).
@ -458,6 +476,7 @@ get_queue_type() ->
Type Type
end. end.
-spec flush_queue(p1_queue:queue()) -> p1_queue:queue().
flush_queue(Q) -> flush_queue(Q) ->
CurrTime = p1_time_compat:monotonic_time(milli_seconds), CurrTime = p1_time_compat:monotonic_time(milli_seconds),
p1_queue:dropwhile( p1_queue:dropwhile(
@ -470,6 +489,7 @@ flush_queue(Q) ->
true true
end, Q). end, Q).
-spec clean_queue(p1_queue:queue(), integer()) -> p1_queue:queue().
clean_queue(Q, CurrTime) -> clean_queue(Q, CurrTime) ->
Q1 = p1_queue:dropwhile( Q1 = p1_queue:dropwhile(
fun({_From, Time}) -> fun({_From, Time}) ->