Skip to content

Commit

Permalink
Added pool initial fill to allow overriding of the initial number of …
Browse files Browse the repository at this point in the history
…items in the pool (defaults to the pool size).
  • Loading branch information
MarkCiliaVincenti committed Nov 27, 2022
1 parent 21ef48b commit 6becb61
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
8 changes: 4 additions & 4 deletions AsyncKeyedLock/AsyncKeyedLock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@
<RepositoryUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock.git</RepositoryUrl>
<PackageProjectUrl>https://github.com/MarkCiliaVincenti/AsyncKeyedLock</PackageProjectUrl>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<Version>5.0.3</Version>
<Version>5.0.4</Version>
<PackageIcon>logo.png</PackageIcon>
<PackageReleaseNotes>Minor performance enhancements.</PackageReleaseNotes>
<PackageReleaseNotes>Added pool initial fill to allow overriding of the initial number of items in the pool (defaults to the pool size).</PackageReleaseNotes>
<Description>An asynchronous .NET Standard 2.0 library that allows you to lock based on a key (keyed semaphores), limiting concurrent threads sharing the same key to a specified number.</Description>
<Copyright>© 2022 Mark Cilia Vincenti</Copyright>
<PackageTags>async,lock,key,semaphore,dictionary,pooling,duplicate</PackageTags>
<RepositoryType>git</RepositoryType>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<AssemblyVersion>5.0.3.0</AssemblyVersion>
<FileVersion>5.0.3.0</FileVersion>
<AssemblyVersion>5.0.4.0</AssemblyVersion>
<FileVersion>5.0.4.0</FileVersion>
<PackageReadmeFile>README.md</PackageReadmeFile>
<IsPackable>true</IsPackable>
<IsTrimmable>true</IsTrimmable>
Expand Down
9 changes: 8 additions & 1 deletion AsyncKeyedLock/AsyncKeyedLockOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ public sealed class AsyncKeyedLockOptions
/// </summary>
public int PoolSize { get; set; } = 0;

/// <summary>
/// The number of items to fill the pool with during initialization. Defaults to -1 (fill up to pool size).
/// </summary>
public int PoolInitialFill { get; set; } = -1;

/// <summary>
/// Initializes options for the <see cref="AsyncKeyedLocker"/> constructors
/// </summary>
/// <param name="maxCount">The maximum number of requests for the semaphore that can be granted concurrently. Defaults to 1.</param>
/// <param name="poolSize">The size of the pool to use in order for generated objects to be reused. Defaults to 0 (disabled).</param>
public AsyncKeyedLockOptions(int maxCount = 1, int poolSize = 0)
/// <param name="poolInitialFill">The number of items to fill the pool with during initialization. Defaults to -1 (fill up to pool size).</param>
public AsyncKeyedLockOptions(int maxCount = 1, int poolSize = 0, int poolInitialFill = -1)
{
MaxCount = maxCount;
PoolSize = poolSize;
PoolInitialFill = poolInitialFill;
}
}
}
16 changes: 13 additions & 3 deletions AsyncKeyedLock/AsyncKeyedLockPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ internal sealed class AsyncKeyedLockPool<TKey>
private readonly BlockingCollection<AsyncKeyedLockReleaser<TKey>> _objects;
private readonly Func<TKey, AsyncKeyedLockReleaser<TKey>> _objectGenerator;

public AsyncKeyedLockPool(Func<TKey, AsyncKeyedLockReleaser<TKey>> objectGenerator, int capacity)
public AsyncKeyedLockPool(Func<TKey, AsyncKeyedLockReleaser<TKey>> objectGenerator, int capacity, int initialFill = -1)
{
_objects = new BlockingCollection<AsyncKeyedLockReleaser<TKey>>(new ConcurrentBag<AsyncKeyedLockReleaser<TKey>>(), capacity);
_objectGenerator = objectGenerator;
for (int i = 0; i < capacity; ++i)
if (initialFill < 0)
{
_objects.Add(_objectGenerator(default));
for (int i = 0; i < capacity; ++i)
{
_objects.Add(_objectGenerator(default));
}
}
else
{
for (int i = 0; i < initialFill; ++i)
{
_objects.Add(_objectGenerator(default));
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ Setting the pool size can be done via the `AsyncKeyedLockOptions` in one of the
var asyncKeyedLocker = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(poolSize: 100));
```

You can also set the initial pool fill (by default this is set to the pool size):

```csharp
var asyncKeyedLocker = new AsyncKeyedLocker<string>(new AsyncKeyedLockOptions(poolSize: 100, poolInitialFill: 50));
```

### Locking
```csharp
using (var lockObj = await asyncKeyedLocker.LockAsync(myObject))
Expand Down

0 comments on commit 6becb61

Please sign in to comment.