Skip to content

Commit

Permalink
refactor(chat): remove duplicated redis operation
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaojiuwo1993 committed Oct 19, 2023
1 parent e15c1a3 commit 10ba463
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public static ChannelMessageBroker AddMessageBrocker(Channel channel)
}
public static void RemoveMessageBrocker(Channel channel)
{
MessageBrokers.TryRemove(channel.Name, out var _);
MessageBrokers.TryRemove(channel.Name, out var broker);
broker.Dispose();
}
}
}
2 changes: 1 addition & 1 deletion src/Servers/Chat/src/Aggregate/Redis/ClientInfoCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public record ClientInfoCache : Core.Abstraction.BaseClass.RedisKeyValueObject
[RedisKey]
public string NickName { get; set; }
public ClientInfo Info { get; set; }
public ClientInfoCache() : base(RedisDbNumber.ChatChannel, TimeSpan.FromHours(1)) { }
public ClientInfoCache() : base(RedisDbNumber.ChatChannel, TimeSpan.FromMinutes(2)) { }
public class RedisClient : Core.Abstraction.BaseClass.RedisClient<ClientInfoCache>
{
public RedisClient() { }
Expand Down
1 change: 1 addition & 0 deletions src/Servers/Chat/src/Application/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public Client(IConnection connection, IServer server, ClientInfo info) : this(co
protected override void EventBinding()
{
base.EventBinding();
// bind a event that can update the client info to redis
_timer = new EasyTimer(TimeSpan.FromMinutes(1));
_timer.Elapsed += (s, e) => Application.StorageOperation.Persistance.UpdateClient(this);
_timer.Start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ protected override void DataOperation()
{
_user = _channel.AddUser(_client, _request.Password ?? null);
}
Aggregate.Channel.UpdateChannelCache(_user, _channel);
base.UpdateChannelCache(); // <= we update the channel cache here
}
else
{
Expand All @@ -106,6 +106,10 @@ protected override void DataOperation()
_result.JoinerPrefix = _client.Info.IRCPrefix;
}

protected override void UpdateChannelCache()
{
// we do not update channel cache again in base class because we update it when channel is created
}
protected override void ResponseConstruct()
{
_response = new JoinResponse(_request, _result);
Expand Down
65 changes: 34 additions & 31 deletions src/Servers/Chat/src/Handler/CmdHandler/Channel/PartHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ public sealed class PartHandler : ChannelHandlerBase
private new PartResponse _response { get => (PartResponse)base._response; set => base._response = value; }
private new PartResult _result { get => (PartResult)base._result; set => base._result = value; }
public PartHandler(IShareClient client, PartRequest request) : base(client, request) { }
static PartHandler()
{
}
protected override void RequestCheck()
{
if (_request.RawRequest is null)
Expand Down Expand Up @@ -48,40 +45,46 @@ protected override void DataOperation()
{
case PeerRoomType.Normal:
case PeerRoomType.Staging:
if (_user.IsChannelCreator)
{
switch (_channel.RoomType)
{
case PeerRoomType.Normal:
case PeerRoomType.Staging:
Aggregate.Channel.RemoveLocalChannel(_channel);
break;
}
foreach (var user in _channel.Users.Values)
{
// we do not need to send part message to leaver
if (user.Client.Info.NickName == _user.Client.Info.NickName)
{
continue;
}
// We create a new KICKHandler to handle KICK operation for us
var kickRequest = new KickRequest
{
KickeeNickName = user.Client.Info.NickName,
ChannelName = _channel.Name,
Reason = _request.Reason,
};
new KickHandler(_client, kickRequest).Handle();
}
}
KickAllUser();
goto default;
default:
// we need always remove the connection in leaver and channel
_channel.RemoveUser(_user);
break;
}
}
protected override void PublishMessage()
private void KickAllUser()
{
if (_user.IsChannelCreator)
{
// we first send all user message to let them known the creator is leaving
foreach (var user in _channel.Users.Values)
{
// we do not need to send part message to leaver
if (user.Client.Info.NickName == _user.Client.Info.NickName)
{
continue;
}
// We create a new KICKHandler to handle KICK operation for us
var kickRequest = new KickRequest
{
KickeeNickName = user.Client.Info.NickName,
ChannelName = _channel.Name,
Reason = _request.Reason,
};
new KickHandler(_client, kickRequest).Handle();
}
// we remove the local channel and unbind events
switch (_channel.RoomType)
{
case PeerRoomType.Normal:
case PeerRoomType.Staging:
Aggregate.Channel.RemoveLocalChannel(_channel);
break;
}
}
}
protected override void UpdateChannelCache()
{
switch (_channel.RoomType)
{
Expand All @@ -90,7 +93,7 @@ protected override void PublishMessage()
Aggregate.Channel.RemoveChannelCache(_user, _channel);
break;
default:
base.PublishMessage();
base.UpdateChannelCache();
break;
}
}
Expand Down

0 comments on commit 10ba463

Please sign in to comment.