Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp

fix(gateway): make presence bus subscribe and unsubscribe async (#3830)

Hampus45ea9ac86191
diff --git a/fluxer_gateway/src/presence/presence_bus.erl b/fluxer_gateway/src/presence/presence_bus.erl
index 6890d98fd..d95b0072f 100644
--- a/fluxer_gateway/src/presence/presence_bus.erl
+++ b/fluxer_gateway/src/presence/presence_bus.erl
@@ -21,15 +21,26 @@ start_link() ->
-spec subscribe(integer()) -> ok.
subscribe(UserId) when is_integer(UserId) ->
- safe_call({subscribe, UserId, self()}).
+ send_request({subscribe, UserId, self()}).
-spec unsubscribe(integer()) -> ok.
unsubscribe(UserId) when is_integer(UserId) ->
- safe_call({unsubscribe, UserId, self()}).
+ send_request({unsubscribe, UserId, self()}).
-spec publish(integer(), term()) -> ok.
publish(UserId, Payload) when is_integer(UserId) ->
- safe_call({publish, UserId, Payload}).
+ send_request({publish, UserId, Payload}).
+
+-spec send_request(term()) -> ok.
+send_request(Request) ->
+ case async_enabled() of
+ true -> gen_server:cast(?MODULE, {async, Request});
+ false -> safe_call(Request)
+ end.
+
+-spec async_enabled() -> boolean().
+async_enabled() ->
+ application:get_env(fluxer_gateway, presence_bus_async, true) =:= true.
-spec diagnostic_info() -> #{atom() => non_neg_integer()}.
diagnostic_info() ->
@@ -85,6 +96,16 @@ merge_shard_diagnostic(Pid, Acc) ->
end.
-spec handle_cast(term(), state()) -> {noreply, state()}.
+handle_cast({async, {subscribe, UserId, Pid} = Request}, State) when
+ is_integer(UserId), is_pid(Pid)
+->
+ {noreply, forward_cast(UserId, Request, State)};
+handle_cast({async, {unsubscribe, UserId, Pid} = Request}, State) when
+ is_integer(UserId), is_pid(Pid)
+->
+ {noreply, forward_cast(UserId, Request, State)};
+handle_cast({async, {publish, UserId, _Payload} = Request}, State) when is_integer(UserId) ->
+ {noreply, forward_cast(UserId, Request, State)};
handle_cast(_Msg, State) ->
{noreply, State}.
@@ -169,6 +190,13 @@ dummy_shard() ->
ok
end.
+-spec forward_cast(term(), term(), state()) -> state().
+forward_cast(Key, Request, State) ->
+ {Index, State1} = ensure_shard(Key, State),
+ Shard = maps:get(Index, maps:get(shards, State1)),
+ gen_server:cast(maps:get(pid, Shard), {async, Request}),
+ State1.
+
-spec forward_call(term(), term(), state()) -> {term(), state()}.
forward_call(Key, Request, State) ->
{Index, State1} = ensure_shard(Key, State),
diff --git a/fluxer_gateway/src/presence/presence_bus_shard.erl b/fluxer_gateway/src/presence/presence_bus_shard.erl
index d1cfdc252..08953527c 100644
--- a/fluxer_gateway/src/presence/presence_bus_shard.erl
+++ b/fluxer_gateway/src/presence/presence_bus_shard.erl
@@ -49,6 +49,15 @@ handle_call(_Request, _From, State) ->
{reply, ok, State}.
-spec handle_cast(term(), state()) -> {noreply, state()}.
+handle_cast({async, {subscribe, UserId, Pid}}, State) when is_integer(UserId), is_pid(Pid) ->
+ _ = do_subscribe(maps:get(scope, State), UserId, Pid),
+ {noreply, State};
+handle_cast({async, {unsubscribe, UserId, Pid}}, State) when is_integer(UserId), is_pid(Pid) ->
+ _ = do_unsubscribe(maps:get(scope, State), UserId, Pid),
+ {noreply, State};
+handle_cast({async, {publish, UserId, Payload}}, State) when is_integer(UserId) ->
+ _ = do_publish(maps:get(scope, State), UserId, Payload),
+ {noreply, State};
handle_cast(_Msg, State) ->
{noreply, State}.