Skip to content
Permalink
Browse files
optimizer code
  • Loading branch information
DLive committed May 16, 2019
1 parent 92ffa38 commit 57938058cfdf8de23e757b733967a18ac734f837
Showing 13 changed files with 74 additions and 187 deletions.
@@ -39,8 +39,8 @@
is_twoway = false ::boolean(),
data ::null|dubbo_rpc_invocation,
mid ::integer(),
mversion ::string(),
error_msg ::string(),
mversion ::binary(),
error_msg ::binary(),
state ::byte(),
decode_state
}).
@@ -74,7 +74,7 @@
category = <<"consumers">> ::binary(),
check=false ::boolean(),
default_timeout=500 ::integer(),
dubbo_version= <<"2.5.3">> ::binary(),
dubbo_version= <<"2.5.3">> ::binary(),
methods=[] ::list(),
revision= <<"">> ::binary(),
side= <<"consumers">> ::binary()
@@ -97,4 +97,7 @@

-record(interface_list,{interface,pid,connection_info}).
-record(provider_node_list,{host_flag,connection_info}).
-record(connection_info,{connection_id,pid,weight,host_flag}).
-record(connection_info,{connection_id,pid,weight,host_flag}).

-type dubbo_request() ::#dubbo_request{}.
-type dubbo_response()::#dubbo_response{}.
@@ -9,4 +9,5 @@
-author("dlive").


-type response_content() :: binary().
-type response_content() :: binary().

@@ -53,14 +53,13 @@ encode(struct, Input, State) ->
{ref, Index} ->
IndexBin = encode(int, Index, State),
<<$Q, IndexBin/binary>>;
_Object ->
logger:debug("[encode] object ~p",[Input]),
encode(class_object, Input, State);
%% {<<>>,State};
{K, V} ->
{BK,SK} = encode(K, State),
{BV,SV} = encode(V, SK),
{<<BK/binary, BV/binary>>, SV}
{<<BK/binary, BV/binary>>, SV};
_Object ->
logger:debug("[encode] object ~p",[Input]),
encode(class_object, Input, State)
end;
encode(int, Int, _State) when Int >= -16, Int =< 47 ->
_Int = Int + 16#90,
@@ -240,10 +239,9 @@ encode(method, Method, _State) when is_binary(Method) ->
Size = size(CamMethod),
<<$m,Size:16/unsigned,CamMethod/binary>>;
encode(method, String, _State) when is_list(String) ->
CamString = erlang_to_camel_case(String),
Length = string:len(CamString),
Bin = list_to_binary(CamString),
<<$m,Length:16/unsigned,Bin/binary>>;
CamMethod = erlang_to_camel_case(String),
Size = size(CamMethod),
<<$m,Size:16/unsigned,CamMethod/binary>>;
encode(reply, ok, _State) ->
<<$H,16#02,16#00,$R,16#01,$N>>;
encode(reply, {ok, Object}, State) ->
@@ -378,11 +376,12 @@ encode(fault, Code, _Error, _Reason, State) ->
%---------------------------------------------------------------------------
erlang_to_camel_case(String) when is_binary(String) ->
AsList = binary_to_list(String),
AsCamel = lists:foldl(fun camelize/2,[],AsList),
list_to_binary(AsCamel);
erlang_to_camel_case(AsList);
erlang_to_camel_case(String) when is_atom(String) ->
AsList = atom_to_list(String),
AsCamel = lists:foldl(fun camelize/2,[],AsList),
erlang_to_camel_case(AsList);
erlang_to_camel_case(String) ->
AsCamel = lists:foldl(fun camelize/2,[],String),
list_to_binary(AsCamel).

camelize(Element,Acc) when Element == $_ -> [$_|Acc];
@@ -13,7 +13,7 @@
%% API
-export([reference/1]).

-spec reference(Data::#dubbo_rpc_invocation{}) -> #dubbo_request{}.
-spec(reference(Data::#dubbo_rpc_invocation{}) -> dubbo_request()).
reference(Data)->
#dubbo_request{
is_event = false,
@@ -83,7 +83,7 @@ encode_response_header(Response,DataLen, ResponseState)->
Header.


-spec decode_header(binary())-> {State::ok|error,Type::request|response,Data::#dubbo_response{}|#dubbo_request{}}.
-spec(decode_header(Header::binary())-> {State::ok|error,Type::request|response,Data::dubbo_response()|dubbo_request()}).
decode_header(Header)->
<<?DUBBO_MEGIC_HIGH,?DUBBO_MEGIC_LOW,Flag:8,State:8,Mid:64,DataLen:32>> = Header,
if
@@ -94,7 +94,7 @@ decode_header(Header)->
{DecodeState,Req} = decode_header(request,Flag,State,Mid,DataLen),
{DecodeState,request,Req}
end.
decode_header(request,Flag,State,Mid,DataLen)->
decode_header(request,Flag,_State,Mid,_DataLen)->
SerializeType = Flag band 16#1f,
IsTwoWay = if
(Flag band 16#40) /=0 -> true;
@@ -112,7 +112,7 @@ decode_header(request,Flag,State,Mid,DataLen)->
serialize_type = SerializeType
},
{ok,Req};
decode_header(response,Flag,State,Mid,DataLen)->
decode_header(response,Flag,State,Mid,_DataLen)->
SerializeType = Flag band 16#1f,
IsEvent = if
(Flag band 16#20) /= 0 -> true;
@@ -255,10 +255,6 @@ select_connection(Interface,RandNum)->
List->
Len = length(List),
RemNum = (RandNum rem Len)+1,
%% RandNum2 = if
%% RandNum==Len -> RandNum-1;
%% true->RandNum
%% end,
InterfaceListItem = lists:nth(RemNum,List),
{ok,InterfaceListItem#interface_list.connection_info}
end.
@@ -12,6 +12,8 @@
-include("dubbo.hrl").
%% API
-export([generate_request/2]).

-spec(generate_request(RequestId::undefined|integer(),NeedResponse::boolean())->{ok,binary()}).
generate_request(undefined,NeedResponse)->
RequestId = dubbo_id_generator:gen_id(),
generate_request(RequestId,NeedResponse);
@@ -52,28 +52,14 @@ invoke_request(Interface,Request,RpcContext,RequestState,CallBackPid)->
end;
{error,none}->
logger:error("[INVOKE] ~p error Reason no_provider",[Interface]),
{error,no_provider};
{error,R1}->
logger:error("[INVOKE] ~p error Reason ~p",[Interface,R1]),
{error,R1}
{error,no_provider}
end.


is_sync(Option)->
maps:is_key(sync,Option).
%% lists:member(sync,Option).
get_ref(Option)->
maps:get(ref,Option,make_ref()).
%% case maps:is_key(ref,Option) of
%% true->
%%
%% end,
%% case proplists:get_value(ref,Option) of
%% undefined->
%% make_ref();
%% Ref->
%% Ref
%% end.

get_timeout(Option)->
maps:get(timeout,Option,?REQUEST_TIME_OUT).
@@ -87,7 +73,8 @@ sync_receive(Ref,TimeOut)->
TimeOut ->
{error,timeout}
end.

merge_attachments(#dubbo_request{data = null}=Request,_Option) ->
Request;
merge_attachments(Request,Option)->
Attachements= Request#dubbo_request.data#dubbo_rpc_invocation.attachments,
case lists:keyfind(attachments,1,Option) of
@@ -73,7 +73,7 @@ init([HostFlag,ProviderConfig,Index]) ->
State = case open(Host,Port) of
{ok,Socket} ->
#state{socket = Socket};
{error}->
{error,_Reason}->
#state{}
end,
NowStamp = time_util:timestamp_ms(),
@@ -236,17 +236,14 @@ open(Host,Port)->
{nodelay, true},
{high_watermark, 512 * 1024},
{low_watermark, 256 * 1024},
%% {high_msgq_watermark,128 * 1024},
%% {low_msgq_watermark,64 * 1024},
{sndbuf, 512 * 1024},
{recbuf, 512 * 1024}
]) of
{ok,Sockets} ->
%% inet:setopts(Sockets, [{active, once}]),
inet:setopts(Sockets, [{active, true}]),
{ok,Sockets};
Info ->
logger:error("start netty client ~p~n",[Info]),
logger:error("start client connection error ~p",[Info]),
{error,Info}
end.

@@ -18,45 +18,34 @@
encode_request_data(Request)->
DataType =case Request#dubbo_request.is_event of
false->
case Request#dubbo_request.data of
#dubbo_rpc_invocation{} ->
dubbo_rpc_invocation;
_ ->
unknow
end;
dubbo_rpc_invocation;
true->
dubbo_event
end,
{ok,Bin} = encode_request_data(DataType,Request,Request#dubbo_request.data,[]),
{ok,Bin}.


encode_request_data(dubbo_rpc_invocation,Request,Data,State) ->
encode_request_data(dubbo_rpc_invocation,_Request,Data,State) ->
RequestList = [
%% jiffy:encode(?DUBBO_VERSION,[]),
string_encode(?DUBBO_VERSION),
?LINE_SEPERATOR,
%% jiffy:encode(Data#dubbo_rpc_invocation.className,[]),
string_encode(Data#dubbo_rpc_invocation.className),
?LINE_SEPERATOR,
%% jiffy:encode(Data#dubbo_rpc_invocation.classVersion,[]),
string_encode(Data#dubbo_rpc_invocation.classVersion),
?LINE_SEPERATOR,
%% jiffy:encode(Data#dubbo_rpc_invocation.methodName,[]),
string_encode(Data#dubbo_rpc_invocation.methodName),
?LINE_SEPERATOR,
%% jiffy:encode(Data#dubbo_rpc_invocation.parameterDesc,[]),
string_encode(Data#dubbo_rpc_invocation.parameterDesc),
?LINE_SEPERATOR
],
{ArgsBin,_} = encode_arguments(Data,State),
AttachBinay = jiffy:encode({Data#dubbo_rpc_invocation.attachments},[]),
RequestData = erlang:iolist_to_binary(RequestList ++ [ArgsBin,AttachBinay,?LINE_SEPERATOR]),
{ok,RequestData};
encode_request_data(dubbo_event,Request,Data,State) ->
encode_request_data(dubbo_event,_Request,Data,_State) ->
%% @todo 确认该数据类型
Bin = jiffy:encode(Data),
%% Bin = cotton_hessian:encode(Data,State),
{ok,Bin}.


@@ -31,9 +31,7 @@ check_goon(Key,Max)->
ets:update_counter(?TRAFFIC_CONTROL,Key,-1),
full;
_V ->
%% logger:debug("check traffic incr value ~p",[V]),
ok

catch
_T:_R->
ets:insert(?TRAFFIC_CONTROL,{Key,1}),

0 comments on commit 5793805

Please sign in to comment.