Fix share sub bug

This commit is contained in:
turtled 2018-08-27 10:15:41 +08:00
parent 1448515e64
commit 95d36d0204
4 changed files with 9 additions and 7 deletions

View File

@ -54,7 +54,7 @@
-type(subid() :: binary() | atom()). -type(subid() :: binary() | atom()).
-type(subopts() :: #{qos => integer(), -type(subopts() :: #{qos => integer(),
share => '$queue' | binary(), share => binary(),
atom() => term()}). atom() => term()}).
-record(subscription, { -record(subscription, {

View File

@ -183,16 +183,18 @@ route([{To, Node}], Delivery) when Node =:= node() ->
route([{To, Node}], Delivery = #delivery{flows = Flows}) when is_atom(Node) -> route([{To, Node}], Delivery = #delivery{flows = Flows}) when is_atom(Node) ->
forward(Node, To, Delivery#delivery{flows = [{route, Node, To}|Flows]}); forward(Node, To, Delivery#delivery{flows = [{route, Node, To}|Flows]});
route([{To, Shared}], Delivery) when is_tuple(Shared); is_binary(Shared) -> route([{To, Group}], Delivery) when is_tuple(Group); is_binary(Group) ->
emqx_shared_sub:dispatch(Shared, To, Delivery); emqx_shared_sub:dispatch(Group, To, Delivery);
route(Routes, Delivery) -> route(Routes, Delivery) ->
lists:foldl(fun(Route, Acc) -> route([Route], Acc) end, Delivery, Routes). lists:foldl(fun(Route, Acc) -> route([Route], Acc) end, Delivery, Routes).
aggre([]) -> aggre([]) ->
[]; [];
aggre([#route{topic = To, dest = Dest}]) -> aggre([#route{topic = To, dest = Node}]) when is_atom(Node) ->
[{To, Dest}]; [{To, Node}];
aggre([#route{topic = To, dest = {Group, _Node}}]) ->
[{To, Group}];
aggre(Routes) -> aggre(Routes) ->
lists:foldl( lists:foldl(
fun(#route{topic = To, dest = Node}, Acc) when is_atom(Node) -> fun(#route{topic = To, dest = Node}, Acc) when is_atom(Node) ->

View File

@ -81,7 +81,7 @@ record(Group, Topic, SubPid) ->
#emqx_shared_subscription{group = Group, topic = Topic, subpid = SubPid}. #emqx_shared_subscription{group = Group, topic = Topic, subpid = SubPid}.
%% TODO: dispatch strategy, ensure the delivery... %% TODO: dispatch strategy, ensure the delivery...
dispatch({Group, _Node}, Topic, Delivery = #delivery{message = Msg, flows = Flows}) -> dispatch(Group, Topic, Delivery = #delivery{message = Msg, flows = Flows}) ->
case pick(subscribers(Group, Topic)) of case pick(subscribers(Group, Topic)) of
false -> Delivery; false -> Delivery;
SubPid -> SubPid ! {dispatch, Topic, Msg}, SubPid -> SubPid ! {dispatch, Topic, Msg},

View File

@ -185,7 +185,7 @@ parse(Topic = <<"$queue/", _/binary>>, #{share := _Group}) ->
parse(Topic = <<"$share/", _/binary>>, #{share := _Group}) -> parse(Topic = <<"$share/", _/binary>>, #{share := _Group}) ->
error({invalid_topic, Topic}); error({invalid_topic, Topic});
parse(<<"$queue/", Topic1/binary>>, Options) -> parse(<<"$queue/", Topic1/binary>>, Options) ->
parse(Topic1, maps:put(share, '$queue', Options)); parse(Topic1, maps:put(share, <<"$queue">>, Options));
parse(<<"$share/", Topic1/binary>>, Options) -> parse(<<"$share/", Topic1/binary>>, Options) ->
[Group, Topic2] = binary:split(Topic1, <<"/">>), [Group, Topic2] = binary:split(Topic1, <<"/">>),
{Topic2, maps:put(share, Group, Options)}; {Topic2, maps:put(share, Group, Options)};