Changed various locking mechanism.

This commit is contained in:
Tom
2025-03-29 20:18:09 +00:00
parent c80f1f2aa0
commit c7904f120d
10 changed files with 282 additions and 143 deletions

View File

@@ -14,7 +14,7 @@ namespace HermesSocketServer.Services
private readonly ServerConfiguration _configuration;
private readonly Serilog.ILogger _logger;
private readonly IDictionary<string, Channel> _channels;
private readonly object _lock;
private readonly Mutex _mutex;
public ChannelManager(IStore<string, User> users, Database database, IStore<string, TTSVoice> voices, ServerConfiguration configuration, Serilog.ILogger logger)
{
@@ -24,7 +24,7 @@ namespace HermesSocketServer.Services
_configuration = configuration;
_logger = logger;
_channels = new ConcurrentDictionary<string, Channel>();
_lock = new object();
_mutex = new Mutex();
}
@@ -36,15 +36,16 @@ namespace HermesSocketServer.Services
return await Task.Run(() =>
{
lock (_lock)
try
{
_mutex.WaitOne();
if (_channels.TryGetValue(userId, out var channel))
return Task.FromResult<Channel?>(channel);
return channel;
var actionTable = _configuration.Database.Tables["Action"];
var chatterTable = _configuration.Database.Tables["Chatter"];
var connectionTable = _configuration.Database.Tables["Connection"];
//var chatterGroupTable = _configuration.Database.Tables["ChatterGroup"];
var connectionStateTable = _configuration.Database.Tables["ConnectionState"];
var groupTable = _configuration.Database.Tables["Group"];
var groupPermissionTable = _configuration.Database.Tables["GroupPermission"];
var policyTable = _configuration.Database.Tables["Policy"];
@@ -93,22 +94,43 @@ namespace HermesSocketServer.Services
]);
_channels.Add(userId, channel);
return Task.FromResult<Channel?>(channel);
return channel;
}
finally
{
_mutex.ReleaseMutex();
}
});
}
public Channel? Get(string channelId)
{
if (_channels.TryGetValue(channelId, out var channel))
return channel;
return null;
try
{
_mutex.WaitOne();
if (_channels.TryGetValue(channelId, out var channel))
return channel;
return null;
}
finally
{
_mutex.ReleaseMutex();
}
}
public async Task Save(string userId)
{
if (!_channels.TryGetValue(userId, out var channel))
return;
Channel? channel;
try
{
_mutex.WaitOne();
if (!_channels.TryGetValue(userId, out channel))
return;
}
finally
{
_mutex.ReleaseMutex();
}
_logger.Debug($"Saving channel data to database [channel id: {channel.Id}][channel name: {channel.User.Name}]");
await Task.WhenAll([