diff --git a/include/agHttpCli.hrl b/include/agHttpCli.hrl index 8010c9f..84e4219 100644 --- a/include/agHttpCli.hrl +++ b/include/agHttpCli.hrl @@ -14,7 +14,7 @@ -define(DEFAULT_IS_RECONNECT, true). -define(DEFAULT_RECONNECT_MAX, 120000). -define(DEFAULT_RECONNECT_MIN, 500). --define(DEFAULT_TIMEOUT, 5000). +-define(DEFAULT_TIMEOUT, infinity). -define(DEFAULT_BODY, undefined). -define(DEFAULT_HEADERS, []). -define(DEFAULT_PID, self()). @@ -49,10 +49,9 @@ }). -record(recvState, { - stage = header :: header | body | done, %% 一个请求收到tcp可能会有多个包 最多分三个阶接收 + stage = header :: header | body | done, %% 一个请求收到tcp可能会有多个包 最多分三个阶接收 contentLength :: undefined | non_neg_integer() | chunked, statusCode :: undefined | 100..505, - reason :: undefined | binary(), headers :: undefined | [binary()], buffer = <<>> :: binary(), body = <<>> :: binary() @@ -86,7 +85,7 @@ port :: 0..65535, hostname :: string(), protocol :: protocol(), - poolSize ::binary(), + poolSize :: binary(), userPassword :: binary() }). diff --git a/rebar.config b/rebar.config index 51ac4c1..5b7b454 100644 --- a/rebar.config +++ b/rebar.config @@ -1 +1,3 @@ +{erl_opts, [{i, "include"}]}. {edoc_opts, [{preprocess, true}]}. + diff --git a/src/httpCli/agAgencyUtils.erl b/src/httpCli/agAgencyUtils.erl index 67bb18b..ce5bfb3 100644 --- a/src/httpCli/agAgencyUtils.erl +++ b/src/httpCli/agAgencyUtils.erl @@ -38,7 +38,7 @@ agencyReply({PidForm, RequestId, TimerRef}, Reply) -> catch PidForm ! #miAgHttpCliRet{requestId = RequestId, reply = Reply}, ok; agencyReply(undefined, RequestRet) -> - ?WARN(not_curInfo ,"not find curInfo ret is:~p~n ",[RequestRet]), + ?WARN(not_curInfo, "not find curInfo ret is:~p~n ", [RequestRet]), ok. -spec agencyReply(undefined | pid(), requestId(), undefined | reference(), term()) -> ok. diff --git a/src/httpCli/agHttpProtocol.erl b/src/httpCli/agHttpProtocol.erl index c8a1229..5e270d5 100644 --- a/src/httpCli/agHttpProtocol.erl +++ b/src/httpCli/agHttpProtocol.erl @@ -59,7 +59,7 @@ response(undefined, Rn, RnRn, Data) -> BodySize == ContentLength -> {done, #recvState{stage = done, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}}; BodySize > ContentLength -> - ?WARN(agTcpAgencyIns, "11 contentLength get to long data why? more: ~p ~n",[BodySize - ContentLength]), + ?WARN(agTcpAgencyIns, "11 contentLength get to long data why? more: ~p ~n", [BodySize - ContentLength]), {done, #recvState{stage = done, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}}; true -> {ok, #recvState{stage = body, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}} @@ -93,10 +93,10 @@ response(#recvState{stage = body, contentLength = ContentLength, body = Body} = BodySize == ContentLength -> {done, RecvState#recvState{stage = done, body = CurData}}; BodySize > ContentLength -> - ?WARN(agTcpAgencyIns, "22 contentLength get to long data why? more: ~p ~n",[BodySize - ContentLength]), + ?WARN(agTcpAgencyIns, "22 contentLength get to long data why? more: ~p ~n", [BodySize - ContentLength]), {done, #recvState{stage = done, body = CurData}}; true -> - {ok,RecvState#recvState{body = CurData}} + {ok, RecvState#recvState{body = CurData}} end; response(#recvState{stage = header, body = OldBody}, Rn, RnRn, Data) -> CurBody = <>, @@ -116,7 +116,7 @@ response(#recvState{stage = header, body = OldBody}, Rn, RnRn, Data) -> BodySize == ContentLength -> {done, #recvState{stage = done, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}}; BodySize > ContentLength -> - ?WARN(agTcpAgencyIns, "33 contentLength get to long data why? more: ~p ~n",[BodySize - ContentLength]), + ?WARN(agTcpAgencyIns, "33 contentLength get to long data why? more: ~p ~n", [BodySize - ContentLength]), {done, #recvState{stage = done, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}}; true -> {ok, #recvState{stage = body, statusCode = StatusCode, headers = Headers, contentLength = ContentLength, body = Body}} diff --git a/src/httpCli/agKvsToBeam.erl b/src/httpCli/agKvsToBeam.erl index 8bdb794..e4932ca 100644 --- a/src/httpCli/agKvsToBeam.erl +++ b/src/httpCli/agKvsToBeam.erl @@ -7,8 +7,9 @@ load/2 ]). --type key() :: atom() | binary() | float() | integer() | list() | tuple(). --type value() :: atom() | binary() | float() | integer() | list() | tuple(). +%% 注意 map类型的数据不能当做key +-type key() :: atom() | binary() | bitstring() | float() | integer() | list() | tuple(). +-type value() :: atom() | binary() | bitstring() | float() | integer() | list() | tuple() | map(). -spec load(term(), [{key(), value()}]) -> ok. load(Module, KVs) -> @@ -19,19 +20,18 @@ load(Module, KVs) -> ok. forms(Module, KVs) -> - Mod = erl_syntax:attribute(erl_syntax:atom(module), - [erl_syntax:atom(Module)]), - ExportList = [erl_syntax:arity_qualifier(erl_syntax:atom(get), - erl_syntax:integer(1))], - Export = erl_syntax:attribute(erl_syntax:atom(export), - [erl_syntax:list(ExportList)]), - Function = erl_syntax:function(erl_syntax:atom(get), - lookup_clauses(KVs)), + %% -module(Module). + Mod = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom(Module)]), + %% -export([get/0]). + ExportList = [erl_syntax:arity_qualifier(erl_syntax:atom(get), erl_syntax:integer(1))], + Export = erl_syntax:attribute(erl_syntax:atom(export), [erl_syntax:list(ExportList)]), + %% get(K) -> V + Function = erl_syntax:function(erl_syntax:atom(get), lookup_clauses(KVs, [])), [erl_syntax:revert(X) || X <- [Mod, Export, Function]]. lookup_clause(Key, Value) -> - Var = to_syntax(Key), - Body = to_syntax(Value), + Var = erl_syntax:abstract(Key), + Body = erl_syntax:abstract(Value), erl_syntax:clause([Var], [], [Body]). lookup_clause_anon() -> @@ -39,24 +39,7 @@ lookup_clause_anon() -> Body = erl_syntax:atom(undefined), erl_syntax:clause([Var], [], [Body]). -lookup_clauses(KVs) -> - lookup_clauses(KVs, []). - lookup_clauses([], Acc) -> lists:reverse(lists:flatten([lookup_clause_anon() | Acc])); lookup_clauses([{Key, Value} | T], Acc) -> - lookup_clauses(T, [lookup_clause(Key, Value) | Acc]). - -to_syntax(Atom) when is_atom(Atom) -> - erl_syntax:atom(Atom); -to_syntax(Binary) when is_binary(Binary) -> - String = erl_syntax:string(binary_to_list(Binary)), - erl_syntax:binary([erl_syntax:binary_field(String)]); -to_syntax(Float) when is_float(Float) -> - erl_syntax:float(Float); -to_syntax(Integer) when is_integer(Integer) -> - erl_syntax:integer(Integer); -to_syntax(List) when is_list(List) -> - erl_syntax:list([to_syntax(X) || X <- List]); -to_syntax(Tuple) when is_tuple(Tuple) -> - erl_syntax:tuple([to_syntax(X) || X <- tuple_to_list(Tuple)]). + lookup_clauses(T, [lookup_clause(Key, Value) | Acc]). \ No newline at end of file diff --git a/src/httpCli/test.erl b/src/httpCli/test.erl index f9ea5b6..1f922f3 100644 --- a/src/httpCli/test.erl +++ b/src/httpCli/test.erl @@ -8,16 +8,16 @@ tt(C, N) -> application:start(erlArango), agHttpCli:startPool(tt, [{poolSize, 100}], []), Request = {<<"GET">>, <<"/_api/database/current">>, [], []}, - io:format("IMY********************** start time ~p~n",[erlang:system_time(millisecond)]), + io:format("IMY********************** start time ~p~n", [erlang:system_time(millisecond)]), [spawn(test, test, [N, Request]) || _Idx <- lists:seq(1, C)]. - %%test(N, Request). +%%test(N, Request). %% /_api/database test(0, Request) -> R1 = {<<"GET">>, <<"/_api/database">>, [], []}, agHttpCli:callAgency(tt, R1, 5000), - io:format("IMY********************** test over ~p~n",[erlang:system_time(millisecond)]); + io:format("IMY********************** test over ~p~n", [erlang:system_time(millisecond)]); test(N, Request) -> erlang:put(cnt, N), agHttpCli:callAgency(tt, Request, 5000),