fix subscription

This commit is contained in:
anlicheng 2026-04-20 20:56:29 +08:00
parent 3f2aa00f35
commit 910987e8e5

View File

@ -88,12 +88,22 @@ handle_call({subscribe, Topic, SubscriberPid}, _From, State = #state{subscribers
Components = of_components(Topic), Components = of_components(Topic),
case is_valid_components(Components) of case is_valid_components(Components) of
true -> true ->
case has_subscription(Topic, SubscriberPid, Subscribers) of
true ->
{reply, ok, State};
false ->
Sub = #subscriber{topic = Topic, subscriber_pid = SubscriberPid, components = Components, order = order_num(Components)}, Sub = #subscriber{topic = Topic, subscriber_pid = SubscriberPid, components = Components, order = order_num(Components)},
%% SubscriberPid的monitor退 %% pid时才建立monitormonitor
erlang:monitor(process, SubscriberPid), case has_subscriber_pid(SubscriberPid, Subscribers) of
true ->
ok;
false ->
erlang:monitor(process, SubscriberPid)
end,
%% %%
RestRemainingMessages = dispatch_remaining_messages(Sub, RemainingMessages), RestRemainingMessages = dispatch_remaining_messages(Sub, RemainingMessages),
{reply, ok, State#state{subscribers = Subscribers ++ [Sub], remaining_messages = RestRemainingMessages}}; {reply, ok, State#state{subscribers = Subscribers ++ [Sub], remaining_messages = RestRemainingMessages}}
end;
false -> false ->
{reply, {error, <<"invalid topic name">>}, State} {reply, {error, <<"invalid topic name">>}, State}
end; end;
@ -114,13 +124,13 @@ handle_call(debug_info, _From, State = #state{subscribers = Subscribers, remaini
handle_cast({publish, Topic, Qos, Content}, State = #state{subscribers = Subscribers, remaining_messages = RemainingMessages}) -> handle_cast({publish, Topic, Qos, Content}, State = #state{subscribers = Subscribers, remaining_messages = RemainingMessages}) ->
MatchedSubscribers = match_subscribers(Subscribers, Topic), MatchedSubscribers = match_subscribers(Subscribers, Topic),
logger:debug("[efka_subscription] topic: ~p, content: ~p, match subscribers: ~p", [Topic, Content, MatchedSubscribers]), logger:debug("[efka_subscription] topic: ~p, content: ~p, match subscribers: ~p", [Topic, Content, MatchedSubscribers]),
case length(MatchedSubscribers) > 0 of case MatchedSubscribers of
true -> [_|_] ->
broadcast(Topic, Content, MatchedSubscribers), broadcast(Topic, Content, MatchedSubscribers),
{noreply, State}; {noreply, State};
false when Qos =:= 0 -> [] when Qos =:= 0 ->
{noreply, State}; {noreply, State};
false -> [] ->
{noreply, State#state{remaining_messages = [{Topic, Content}|RemainingMessages]}} {noreply, State#state{remaining_messages = [{Topic, Content}|RemainingMessages]}}
end. end.
@ -178,6 +188,17 @@ match_subscribers(Subscribers, Topic) when is_list(Subscribers), is_binary(Topic
contain_channel(Pid, Subscribers) when is_pid(Pid), is_list(Subscribers) -> contain_channel(Pid, Subscribers) when is_pid(Pid), is_list(Subscribers) ->
lists:search(fun(#subscriber{subscriber_pid = Pid0}) -> Pid == Pid0 end, Subscribers) /= false. lists:search(fun(#subscriber{subscriber_pid = Pid0}) -> Pid == Pid0 end, Subscribers) /= false.
-spec has_subscriber_pid(pid(), [#subscriber{}]) -> boolean().
has_subscriber_pid(SubscriberPid, Subscribers) when is_pid(SubscriberPid), is_list(Subscribers) ->
lists:any(fun(#subscriber{subscriber_pid = SubscriberPid0}) -> SubscriberPid =:= SubscriberPid0 end, Subscribers).
-spec has_subscription(binary(), pid(), [#subscriber{}]) -> boolean().
has_subscription(Topic, SubscriberPid, Subscribers)
when is_binary(Topic), is_pid(SubscriberPid), is_list(Subscribers) ->
lists:any(fun(#subscriber{topic = Topic0, subscriber_pid = SubscriberPid0}) ->
Topic =:= Topic0 andalso SubscriberPid =:= SubscriberPid0
end, Subscribers).
%% topic和发布的topic的Components信息 %% topic和发布的topic的Components信息
%% *++ %% *++
-spec match_components(list(), list()) -> boolean(). -spec match_components(list(), list()) -> boolean().
@ -227,7 +248,7 @@ dispatch_remaining_messages(#subscriber{subscriber_pid = SubscriberPid, componen
%% %%
lists:foldl(fun({Topic0, Content0}, Acc) -> lists:foldl(fun({Topic0, Content0}, Acc) ->
Components0 = of_components(Topic0), Components0 = of_components(Topic0),
case match_components(Components0, Components) of case match_components(Components, Components0) of
true -> true ->
SubscriberPid ! {topic_broadcast, Topic0, Content0}, SubscriberPid ! {topic_broadcast, Topic0, Content0},
Acc; Acc;