###ThrottlerPurger
ThrottlerPurger
The Purge()
method is looking somehow strange. Why do you create the Dictionary<string, IThrottler> toRemove
outside of the lock
and why do you assign null
to it ?
The following is doing the same but cleaner
public void Purge()
{
lock (throttlers)
{
var toRemove = new Dictionary<string, IThrottler>();
foreach (var throttler in throttlers)
{
if (throttler.Value.IsIdle || !throttler.Value.IsAlive)
{
toRemove.Add(throttler.Key, throttler.Value);
}
}
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
and if IThrottlerCollection
is implementing IEnumerable<T>
we can make it shine like so
public void Purge()
{
lock (throttlers)
{
var toRemove = throttlers.Where(throttler => IsRemoveable(throttler))
.ToDictionary(throttler => throttler.Key, throttler => throttler .Value);
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
private bool IsRemoveable(IThrottler throttler)
{
return throttler.Value.IsIdle || !throttler.Value.IsAlive;
}
###ThrottlerPurger
The Purge()
method is looking somehow strange. Why do you create the Dictionary<string, IThrottler> toRemove
outside of the lock
and why do you assign null
to it ?
The following is doing the same but cleaner
public void Purge()
{
lock (throttlers)
{
var toRemove = new Dictionary<string, IThrottler>();
foreach (var throttler in throttlers)
{
if (throttler.Value.IsIdle || !throttler.Value.IsAlive)
{
toRemove.Add(throttler.Key, throttler.Value);
}
}
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
and if IThrottlerCollection
is implementing IEnumerable<T>
we can make it shine like so
public void Purge()
{
lock (throttlers)
{
var toRemove = throttlers.Where(throttler => IsRemoveable(throttler))
.ToDictionary(throttler => throttler.Key, throttler => throttler .Value);
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
private bool IsRemoveable(IThrottler throttler)
{
return throttler.Value.IsIdle || !throttler.Value.IsAlive;
}
ThrottlerPurger
The Purge()
method is looking somehow strange. Why do you create the Dictionary<string, IThrottler> toRemove
outside of the lock
and why do you assign null
to it ?
The following is doing the same but cleaner
public void Purge()
{
lock (throttlers)
{
var toRemove = new Dictionary<string, IThrottler>();
foreach (var throttler in throttlers)
{
if (throttler.Value.IsIdle || !throttler.Value.IsAlive)
{
toRemove.Add(throttler.Key, throttler.Value);
}
}
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
and if IThrottlerCollection
is implementing IEnumerable<T>
we can make it shine like so
public void Purge()
{
lock (throttlers)
{
var toRemove = throttlers.Where(throttler => IsRemoveable(throttler))
.ToDictionary(throttler => throttler.Key, throttler => throttler .Value);
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
private bool IsRemoveable(IThrottler throttler)
{
return throttler.Value.IsIdle || !throttler.Value.IsAlive;
}
###ThrottlerPurger
The Purge()
method is looking somehow strange. Why do you create the Dictionary<string, IThrottler> toRemove
outside of the lock
and why do you assign null
to it ?
The following is doing the same but cleaner
public void Purge()
{
lock (throttlers)
{
var toRemove = new Dictionary<string, IThrottler>();
foreach (var throttler in throttlers)
{
if (throttler.Value.IsIdle || !throttler.Value.IsAlive)
{
toRemove.Add(throttler.Key, throttler.Value);
}
}
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
and if IThrottlerCollection
is implementing IEnumerable<T>
we can make it shine like so
public void Purge()
{
lock (throttlers)
{
var toRemove = throttlers.Where(throttler => IsRemoveable(throttler))
.ToDictionary(throttler => throttler.Key, throttler => throttler .Value);
foreach (var throttler in toRemove)
{
throttlers.Remove(throttler.Key);
logger.LogInfo("Purged {0} {1} throttler.", throttler.Value.IsAlive ? "idle" : "dead", throttler.Key);
throttler.Value.Stop();
}
}
}
private bool IsRemoveable(IThrottler throttler)
{
return throttler.Value.IsIdle || !throttler.Value.IsAlive;
}