Skip to main content
Code Review

Return to Answer

Commonmark migration
Source Link

###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;
}
Source Link
Heslacher
  • 50.9k
  • 5
  • 83
  • 177

###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;
}
lang-cs

AltStyle によって変換されたページ (->オリジナル) /