Skip to content

Commit

Permalink
Fix #13 KeyNotFoundException by using ConcurrentDictionary.GetOrAdd i…
Browse files Browse the repository at this point in the history
…nstead of indexer
  • Loading branch information
FrankBakkerNl committed Oct 13, 2017
1 parent 98e68eb commit 36b943b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/JSPool/JsPool.cs
Expand Up @@ -33,7 +33,7 @@ public class JsPool<TPooled, TOriginal> : IJsPool<TPooled> where TPooled : Poole
/// <summary>
/// Metadata for the engines. Total number of engines that have been created is reflected in its Count.
/// </summary>
protected readonly IDictionary<TPooled, EngineMetadata> _metadata = new ConcurrentDictionary<TPooled, EngineMetadata>();
protected readonly ConcurrentDictionary<TPooled, EngineMetadata> _metadata = new ConcurrentDictionary<TPooled, EngineMetadata>();
/// <summary>
/// Factory method used to create engines.
/// </summary>
Expand Down Expand Up @@ -179,8 +179,9 @@ public virtual TPooled GetEngine(TimeSpan? timeout = null)
/// <param name="engine"></param>
private TPooled TakeEngine(TPooled engine)
{
var metadata = _metadata[engine];
metadata.InUse = true;
var metadata = _metadata.GetOrAdd(engine, _ => new EngineMetadata());

metadata.InUse = true;
metadata.UsageCount++;
return engine;
}
Expand Down Expand Up @@ -247,7 +248,7 @@ public virtual void DisposeEngine(TPooled engine, bool repopulateEngines = true)
{
((IDisposable)engine.InnerEngine).Dispose();
}
_metadata.Remove(engine);
_metadata.TryRemove(engine, out var _);

if (repopulateEngines)
{
Expand Down

0 comments on commit 36b943b

Please sign in to comment.