Changed various locking mechanisms.
This commit is contained in:
@ -27,7 +27,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
private readonly AudioPlaybackEngine _playback;
|
||||
private readonly ILogger _logger;
|
||||
private readonly Random _random;
|
||||
private readonly object _lock;
|
||||
private readonly ReaderWriterLockSlim _rwls;
|
||||
|
||||
|
||||
public RedemptionManager(
|
||||
@ -50,7 +50,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
_playback = playback;
|
||||
_logger = logger;
|
||||
_random = new Random();
|
||||
_lock = new object();
|
||||
_rwls = new ReaderWriterLockSlim();
|
||||
|
||||
var topic = _bus.GetTopic("redemptions_initiation");
|
||||
topic.Subscribe(data =>
|
||||
@ -110,16 +110,15 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
|
||||
private void Add(string twitchRedemptionId, string redemptionId)
|
||||
{
|
||||
lock (_lock)
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (!_redeems.TryGetValue(twitchRedemptionId, out var redeems))
|
||||
_redeems.Add(twitchRedemptionId, redeems = new List<string>());
|
||||
|
||||
var item = _redemptions.TryGetValue(redemptionId, out var r) ? r : null;
|
||||
if (item == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var redemptions = redeems.Select(r => _redemptions.TryGetValue(r, out var rr) ? rr : null);
|
||||
bool added = false;
|
||||
@ -138,12 +137,17 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
if (!added)
|
||||
redeems.Add(redemptionId);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
_logger.Debug($"Added redemption action [redemption id: {redemptionId}][twitch redemption id: {twitchRedemptionId}]");
|
||||
}
|
||||
|
||||
private void Add(string twitchRedemptionId, Redemption item)
|
||||
{
|
||||
lock (_lock)
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (!_redeems.TryGetValue(twitchRedemptionId, out var redemptionNames))
|
||||
_redeems.Add(twitchRedemptionId, redemptionNames = new List<string>());
|
||||
@ -165,6 +169,10 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
if (!added)
|
||||
redemptionNames.Add(item.Id);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
_logger.Debug($"Added redemption action [redemption id: {item.Id}][twitch redemption id: {twitchRedemptionId}]");
|
||||
}
|
||||
|
||||
@ -419,7 +427,8 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
|
||||
public IEnumerable<RedeemableAction> Get(string twitchRedemptionId)
|
||||
{
|
||||
lock (_lock)
|
||||
_rwls.EnterReadLock();
|
||||
try
|
||||
{
|
||||
if (_redeems.TryGetValue(twitchRedemptionId, out var redemptionIds))
|
||||
return redemptionIds.Select(r => _redemptions.TryGetValue(r, out var redemption) ? redemption : null)
|
||||
@ -427,15 +436,19 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
.Select(r => _actions.TryGetValue(r!.ActionName, out var action) ? action : null)
|
||||
.Where(a => a != null)!;
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitReadLock();
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public void Initialize()
|
||||
{
|
||||
_logger.Debug($"Redemption manager is about to initialize [redemption count: {_redemptions.Count()}][action count: {_actions.Count}]");
|
||||
|
||||
lock (_lock)
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
_logger.Debug($"Redemption manager is about to initialize [redemption count: {_redemptions.Count()}][action count: {_actions.Count}]");
|
||||
_redeems.Clear();
|
||||
|
||||
var ordered = _redemptions.Select(r => r.Value).Where(r => r != null).OrderBy(r => r.Order);
|
||||
@ -463,18 +476,31 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
}
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
|
||||
_logger.Debug("All redemptions added. Redemption Manager is ready.");
|
||||
}
|
||||
|
||||
public bool RemoveAction(string actionName)
|
||||
{
|
||||
return _actions.Remove(actionName);
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
return _actions.Remove(actionName);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
}
|
||||
|
||||
public bool RemoveRedemption(string redemptionId)
|
||||
{
|
||||
lock (_lock)
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (!_redemptions.TryGetValue(redemptionId, out var redemption))
|
||||
{
|
||||
@ -490,6 +516,10 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
return true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -507,7 +537,8 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
|
||||
public bool Update(Redemption redemption)
|
||||
{
|
||||
lock (_lock)
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
if (_redemptions.TryGetValue(redemption.Id, out var r))
|
||||
{
|
||||
@ -548,6 +579,10 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
return true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
|
||||
_logger.Warning($"Cannot find redemption by name [redemption id: {redemption.Id}][redemption action: {redemption.ActionName}]");
|
||||
return false;
|
||||
@ -555,12 +590,20 @@ namespace TwitchChatTTS.Twitch.Redemptions
|
||||
|
||||
public bool Update(RedeemableAction action)
|
||||
{
|
||||
if (_actions.TryGetValue(action.Name, out var a))
|
||||
_rwls.EnterWriteLock();
|
||||
try
|
||||
{
|
||||
a.Type = action.Type;
|
||||
a.Data = action.Data;
|
||||
_logger.Debug($"Updated redeemable action in redemption manager [action name: {action.Name}]");
|
||||
return true;
|
||||
if (_actions.TryGetValue(action.Name, out var a))
|
||||
{
|
||||
a.Type = action.Type;
|
||||
a.Data = action.Data;
|
||||
_logger.Debug($"Updated redeemable action in redemption manager [action name: {action.Name}]");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
_rwls.ExitWriteLock();
|
||||
}
|
||||
|
||||
_logger.Warning($"Cannot find redeemable action by name [action name: {action.Name}]");
|
||||
|
Reference in New Issue
Block a user