Skip to content

Commit

Permalink
Merge pull request #42 from gilbertwong96/hotfix/wrong_function_spec
Browse files Browse the repository at this point in the history
fix type spec to make dialyzer pass
  • Loading branch information
zhongwencool authored Feb 12, 2022
2 parents 527a931 + cbdc405 commit 481cb44
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 23 deletions.
19 changes: 17 additions & 2 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{gun, "1.3.3"}
]}.


{gpb_opts, [
{recursive, false},
{i, "priv/protos"},
Expand All @@ -26,5 +25,21 @@

{plugins, [
{rebar3_gpb_plugin, "2.13.1"},
{rebar3_eetcd_plugin, "0.3.1"}
{rebar3_eetcd_plugin, "0.3.2"}
]}.

{xref_checks, [
undefined_function_calls,
undefined_functions,
locals_not_used,
deprecated_function_calls,
warnings_as_errors,
deprecated_functions
]}.

{xref_ignores, [
auth_pb,
gogo_pb,
kv_pb,
router_pb
]}.
2 changes: 1 addition & 1 deletion src/clients/eetcd_election_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ leader(Request) ->
eetcd_stream:unary(Request, 'Etcd.LeaderRequest', <<"/v3electionpb.Election/Leader">>, 'Etcd.LeaderResponse').

%% @doc Stream RPC
-spec observe(router_pb:'Etcd.LeaderRequest'()) ->
-spec observe(atom()|reference()) ->
{ok, GunPid :: pid(), Http2Ref:: reference()}|{error,eetcd:eetcd_error()}.
observe(Request) ->
eetcd_stream:new(Request, <<"/v3electionpb.Election/Observe">>).
Expand Down
2 changes: 1 addition & 1 deletion src/clients/eetcd_lease_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ lease_revoke(Request) ->
eetcd_stream:unary(Request, 'Etcd.LeaseRevokeRequest', <<"/etcdserverpb.Lease/LeaseRevoke">>, 'Etcd.LeaseRevokeResponse').

%% @doc Stream RPC
-spec lease_keep_alive(router_pb:'Etcd.LeaseKeepAliveRequest'()) ->
-spec lease_keep_alive(atom()|reference()) ->
{ok, GunPid :: pid(), Http2Ref:: reference()}|{error,eetcd:eetcd_error()}.
lease_keep_alive(Request) ->
eetcd_stream:new(Request, <<"/etcdserverpb.Lease/LeaseKeepAlive">>).
Expand Down
2 changes: 1 addition & 1 deletion src/clients/eetcd_watch_gen.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-export([watch/1]).

%% @doc Stream RPC
-spec watch(router_pb:'Etcd.WatchRequest'()) ->
-spec watch(atom()|reference()) ->
{ok, GunPid :: pid(), Http2Ref:: reference()}|{error,eetcd:eetcd_error()}.
watch(Request) ->
eetcd_stream:new(Request, <<"/etcdserverpb.Watch/Watch">>).
Expand Down
2 changes: 1 addition & 1 deletion src/eetcd.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ info() ->
ok.

%%% @doc Create context for request.
-spec new(atom()|reference()) -> context().
-spec new(atom()|reference()|context()) -> context().
new(ConnName) when is_atom(ConnName) orelse is_reference(ConnName) -> #{eetcd_conn_name => ConnName};
new(Context) when is_map(Context) -> Context.

Expand Down
4 changes: 2 additions & 2 deletions src/eetcd_election.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
-type campaign_ctx() :: #{campaign => map()|'waiting_campaign_response', http2_pid => pid(), monitor_ref => reference(), stream_ref => reference()}.

%%% @doc Creates a blank context for a request.
-spec new(atom()|reference()) -> context().
-spec new(name()|context()) -> context().
new(Ctx) -> eetcd:new(Ctx).

%% @doc Timeout is an integer greater than zero which specifies how many milliseconds to wait for a reply,
Expand Down Expand Up @@ -163,7 +163,7 @@ campaign_response(CCtx, Msg) ->
proclaim(Ctx) ->
eetcd_election_gen:proclaim(Ctx).

-spec proclaim(Ctx :: context()|name(), Leader :: map(), Value :: binary()) ->
-spec proclaim(Ctx :: context()|name(), Leader :: binary(), Value :: binary()) ->
{ok, router_pb:'Etcd.ProclaimResponse'()} | {error, eetcd_error()}.
proclaim(Ctx, Leader, Val) ->
Ctx1 = new(Ctx),
Expand Down
3 changes: 1 addition & 2 deletions src/eetcd_grpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ encode(GrpcType, Msg, MsgName) ->
PbMsg = router_pb:encode_msg(Msg, MsgName, [{verify, true}]),
encode_(GrpcType, PbMsg).

-spec decode(identity | gzip, binary(), atom()) -> grpc_status().
-spec decode(identity | gzip, binary(), atom()) -> {ok, map(), binary()} | more.
decode(Encoding, Frame, PbType) ->
case decode_(Frame, Encoding) of
{ok, PbBin, Fragment} ->
{ok, router_pb:decode_msg(PbBin, PbType), Fragment};
more -> more
end.


grpc_status(RespHeaders) ->
GrpcStatus = binary_to_integer(proplists:get_value(<<"grpc-status">>, RespHeaders, <<"0">>)),
GrpcMessage = proplists:get_value(<<"grpc-message">>, RespHeaders, <<"">>),
Expand Down
2 changes: 1 addition & 1 deletion src/eetcd_kv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ with_range_end(Context, End) ->

%% @doc Limit the number of results to return from `get' request.
%% If with_limit is given a 0 limit, it is treated as no limit.
-spec with_limit(context(), iodata()) -> context().
-spec with_limit(context(), non_neg_integer()) -> context().
with_limit(Context, End) ->
maps:put(limit, End, Context).

Expand Down
4 changes: 2 additions & 2 deletions src/eetcd_lease.erl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-define(TRY_RECONNECTING, try_reconnecting).

%%% @doc Create context for request.
-spec new(atom()|reference()) -> context().
-spec new(name()|context()) -> context().
new(Context) -> eetcd:new(Context).

%% @doc Timeout is an integer greater than zero which specifies how many milliseconds to wait for a reply,
Expand All @@ -34,7 +34,7 @@ grant(Context, TTL) ->
eetcd_lease_gen:lease_grant(C2).

%% @doc Revoke revokes the given lease.
-spec revoke(context(), pos_integer()) ->
-spec revoke(name()|context(), pos_integer()) ->
{ok, router_pb:'Etcd.LeaseGrantResponse'()}|{error, eetcd_error()}.
revoke(Context, LeaseID) ->
C1 = new(Context),
Expand Down
23 changes: 15 additions & 8 deletions src/eetcd_maintenance.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-export([defragment/3, status/3, hash_kv/4, move_leader/2]).

%%% @doc AlarmList gets all active alarms.
-spec alarm_list(context()) ->
-spec alarm_list(name()|context()) ->
{ok,router_pb:'Etcd.AlarmResponse'()}|{error,eetcd_error()}.
alarm_list(ConnName) ->
C1 = eetcd:new(ConnName),
Expand All @@ -15,7 +15,7 @@ alarm_list(ConnName) ->
eetcd_maintenance_gen:alarm(C4).

%%% @doc AlarmDisarm disarms a given alarm.
-spec alarm_disarm(context(), integer(), integer()) ->
-spec alarm_disarm(name()|context(), integer(), integer()) ->
{ok,router_pb:'Etcd.AlarmResponse'()}|{error,eetcd_error()}.
alarm_disarm(Context, MemberId, Alarm) ->
C1 = eetcd:new(Context),
Expand All @@ -25,14 +25,21 @@ alarm_disarm(Context, MemberId, Alarm) ->
eetcd_maintenance_gen:alarm(C4).

%%% @doc AlarmDisarmAll disarms all alarm.
-spec alarm_disarm_all(context()) ->
{ok,router_pb:'Etcd.AlarmResponse'()}|{error,eetcd_error()}.
-spec alarm_disarm_all(name()|context()) ->
router_pb:'Etcd.AlarmResponse'().
alarm_disarm_all(ConnName) ->
{ok, Acc0 = #{alarms := List}} = alarm_list(ConnName),
lists:foldl(fun(#{memberID := Id, alarm := Alarm}, Acc) ->
#{alarm := Old} = Acc,
{ok, A = #{alarm := L}} = alarm_disarm(ConnName, Id, Alarm),
A#{alarm => L ++ Old} end, Acc0#{alarm => []}, List).
lists:foldl(
fun(#{memberID := Id, alarm := Alarm}, Acc) ->
#{alarms := Old} = Acc,
case alarm_disarm(ConnName, Id, Alarm) of
{ok, #{alarms := L}} ->
Acc#{alarms => L ++ Old};
{error, Reason} ->
?LOG_ERROR("~p disarm ~p failed by ~p ", [ConnName, Alarm, Reason]),
Acc
end
end, Acc0#{alarms => []}, List).

%%% @doc Defragment releases wasted space from internal fragmentation on a given etcd member.
%%% Defragment is only needed when deleting a large number of keys and want to reclaim the resources.
Expand Down
4 changes: 2 additions & 2 deletions src/eetcd_stream.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ new(Name, Msg, MsgName, Path) ->
Err -> Err
end.

-spec data(GunPid, Http2Ref, EtcdMsg, EtcdMsgName, Http2Path) -> Http2Ref when
-spec data(GunPid, Http2Ref, EtcdMsg, EtcdMsgName, IsFin) -> Http2Ref when
GunPid :: pid(),
Http2Ref :: reference(),
EtcdMsg :: map(),
EtcdMsgName :: atom(),
Http2Path :: iodata().
IsFin :: fin | nofin.
data(Pid, Ref, Msg, MsgName, IsFin) ->
EncodeBody = eetcd_grpc:encode(identity, Msg, MsgName),
gun:data(Pid, Ref, IsFin, EncodeBody),
Expand Down

0 comments on commit 481cb44

Please sign in to comment.