AsyncResourcePool 1.0.0

Non-blocking, managed pool of re-usable resources, with a specified minimum and optional maximum number of resources, and optional expiry time for resources to be cleaned up if not used for a time.

Install-Package AsyncResourcePool -Version 1.0.0
dotnet add package AsyncResourcePool --version 1.0.0
<PackageReference Include="AsyncResourcePool" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AsyncResourcePool --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

AsyncResourcePool

Non-blocking, managed pool of re-usable resources, with a specified minimum and optional maximum number of resources, and optional expiry time for resources to be cleaned up if not used for a time.

Installation

dotnet add package AsyncResourcePool

Example usage: Connection Pool for Snowflake Connector for .NET

https://github.com/snowflakedb/snowflake-connector-net

  1. Define a ConnectionPool class which consumes AsyncResourcePool internally
public sealed class ConnectionPool
{
    private readonly IAsyncResourcePool<SnowflakeDbConnection> _resourcePool;

    public ConnectionPool(string connectionString)
    {
        var connectionFactory = GetConnectionFactoryFunc(connectionString);

        var asyncResourcePoolOptions = new AsyncResourcePoolOptions(
            minNumResources: 20,
            resourcesExpireAfter: TimeSpan.FromMinutes(15));

        _resourcePool = new AsyncResourcePool<SnowflakeDbConnection>(connectionFactory, asyncResourcePoolOptions);
    }

    public Task<ReusableResource<SnowflakeDbConnection>> Get(CancellationToken cancellationToken) => _resourcePool.Get(cancellationToken);

    public void Dispose() => _resourcePool.Dispose();

    private static Func<Task<SnowflakeDbConnection>> GetConnectionFactoryFunc(string connectionString)
    {
        return async () =>
        {
            var conn = new SnowflakeDbConnection
            {
                ConnectionString = connectionString
            };

            await conn.OpenAsync();

            return conn;
        };
    }
}
  1. Optionally register ConnectionPool as Singleton in dependency injection configuration
services.AddSingleton<ConnectionPool>(sp => new ConnectionPool(...));
  1. Consume ConnectionPool
public class ConnectionConsumer
{
    private readonly ConnectionPool _connectionPool;

    public ConnectionConsumer(ConnectionPool connectionPool) {
        _connectionPool = connectionPool;
    }

    public async Task DoSomething(CancellationToken cancellationToken) {
        using (var reusableConnection = await _connectionPool.Get(cancellationToken)) {
            var connection = reusableConnection.Resource;
            // Do something with the connection
        }
    }
} 

AsyncResourcePool

Non-blocking, managed pool of re-usable resources, with a specified minimum and optional maximum number of resources, and optional expiry time for resources to be cleaned up if not used for a time.

Installation

dotnet add package AsyncResourcePool

Example usage: Connection Pool for Snowflake Connector for .NET

https://github.com/snowflakedb/snowflake-connector-net

  1. Define a ConnectionPool class which consumes AsyncResourcePool internally
public sealed class ConnectionPool
{
    private readonly IAsyncResourcePool<SnowflakeDbConnection> _resourcePool;

    public ConnectionPool(string connectionString)
    {
        var connectionFactory = GetConnectionFactoryFunc(connectionString);

        var asyncResourcePoolOptions = new AsyncResourcePoolOptions(
            minNumResources: 20,
            resourcesExpireAfter: TimeSpan.FromMinutes(15));

        _resourcePool = new AsyncResourcePool<SnowflakeDbConnection>(connectionFactory, asyncResourcePoolOptions);
    }

    public Task<ReusableResource<SnowflakeDbConnection>> Get(CancellationToken cancellationToken) => _resourcePool.Get(cancellationToken);

    public void Dispose() => _resourcePool.Dispose();

    private static Func<Task<SnowflakeDbConnection>> GetConnectionFactoryFunc(string connectionString)
    {
        return async () =>
        {
            var conn = new SnowflakeDbConnection
            {
                ConnectionString = connectionString
            };

            await conn.OpenAsync();

            return conn;
        };
    }
}
  1. Optionally register ConnectionPool as Singleton in dependency injection configuration
services.AddSingleton<ConnectionPool>(sp => new ConnectionPool(...));
  1. Consume ConnectionPool
public class ConnectionConsumer
{
    private readonly ConnectionPool _connectionPool;

    public ConnectionConsumer(ConnectionPool connectionPool) {
        _connectionPool = connectionPool;
    }

    public async Task DoSomething(CancellationToken cancellationToken) {
        using (var reusableConnection = await _connectionPool.Get(cancellationToken)) {
            var connection = reusableConnection.Resource;
            // Do something with the connection
        }
    }
} 

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 77 6/29/2019