Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
<!-- General information -->
<PropertyGroup>
<Authors>Ugo Lattanzi</Authors>
<VersionPrefix>6.3.5</VersionPrefix>
<!--
<VersionPrefix>6.3.7</VersionPrefix>
<VersionSuffix>pre</VersionSuffix>
-->

<!-- <TargetFrameworks>netstandard2.0;net461;net472;netcoreapp3.0;netcoreapp3.1</TargetFrameworks> -->
<TargetFrameworks>netstandard2.0;netcoreapp3.0;netcoreapp3.1</TargetFrameworks>
<IncludeSource>True</IncludeSource>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
Expand All @@ -14,7 +16,7 @@ namespace StackExchange.Redis.Extensions.Core.Implementations
/// <inheritdoc/>
public class RedisCacheConnectionPoolManager : IRedisCacheConnectionPoolManager
{
private readonly ConcurrentBag<Lazy<IStateAwareConnection>> connections;
private readonly ConcurrentBag<IStateAwareConnection> connections;
private readonly RedisConfiguration redisConfiguration;
private readonly ILogger<RedisCacheConnectionPoolManager> logger;

Expand All @@ -26,18 +28,16 @@ public class RedisCacheConnectionPoolManager : IRedisCacheConnectionPoolManager
public RedisCacheConnectionPoolManager(RedisConfiguration redisConfiguration, ILogger<RedisCacheConnectionPoolManager> logger = null)
{
this.redisConfiguration = redisConfiguration ?? throw new ArgumentNullException(nameof(redisConfiguration));

this.connections = new ConcurrentBag<Lazy<IStateAwareConnection>>();
this.connections = new ConcurrentBag<IStateAwareConnection>();
this.logger = logger ?? NullLogger<RedisCacheConnectionPoolManager>.Instance;
this.EmitConnections();
}

/// <inheritdoc/>
public void Dispose()
{
var activeConnections = this.connections.Where(lazy => lazy.IsValueCreated).ToList();

foreach (var connection in activeConnections)
connection.Value.Dispose();
foreach (var connection in connections)
connection.Dispose();

while (this.connections.IsEmpty == false)
this.connections.TryTake(out var taken);
Expand All @@ -46,32 +46,23 @@ public void Dispose()
/// <inheritdoc/>
public IConnectionMultiplexer GetConnection()
{
this.EmitConnections();

var loadedLazies = this.connections.Where(lazy => lazy.IsValueCreated);

if (loadedLazies.Count() == this.connections.Count)
return this.connections.OrderBy(x => x.Value.TotalOutstanding()).First().Value.Connection;
if (this.connections.IsEmpty == false)
{
return this.connections.OrderBy(c => c.TotalOutstanding()).First().Connection;
}

return this.connections.First(lazy => !lazy.IsValueCreated).Value.Connection;
throw new Exception("no connection available");
}

/// <inheritdoc/>
public ConnectionPoolInformation GetConnectionInformations()
{
var activeConnections = 0;
var invalidConnections = 0;
var readyNotUsedYet = 0;

foreach (var lazy in connections)
{
if (!lazy.IsValueCreated)
{
readyNotUsedYet++;
continue;
}

if (!lazy.Value.IsConnected())
if (!lazy.IsConnected())
{
invalidConnections++;
continue;
Expand All @@ -85,35 +76,30 @@ public ConnectionPoolInformation GetConnectionInformations()
RequiredPoolSize = redisConfiguration.PoolSize,
ActiveConnections = activeConnections,
InvalidConnections = invalidConnections,
ReadyNotUsedYet = readyNotUsedYet
ReadyNotUsedYet = 0
};
}

private void EmitConnection()
private Task EmitConnection()
{
this.connections.Add(new Lazy<IStateAwareConnection>(() =>
{
this.logger.LogDebug("Creating new Redis connection.");

var multiplexer = ConnectionMultiplexer.Connect(redisConfiguration.ConfigurationOptions);

if (this.redisConfiguration.ProfilingSessionProvider != null)
multiplexer.RegisterProfiler(this.redisConfiguration.ProfilingSessionProvider);

return this.redisConfiguration.StateAwareConnectionFactory(multiplexer, logger);
}));
return Task.Run(
async () =>
{
this.logger.LogDebug("Creating new Redis connection.");
var multiplexer = await ConnectionMultiplexer.ConnectAsync(redisConfiguration.ConfigurationOptions);
if (this.redisConfiguration.ProfilingSessionProvider != null)
multiplexer.RegisterProfiler(this.redisConfiguration.ProfilingSessionProvider);
this.connections.Add(this.redisConfiguration.StateAwareConnectionFactory(multiplexer, logger));
});
}

private void EmitConnections()
{
if (connections.Count >= this.redisConfiguration.PoolSize)
return;

for (var i = 0; i < this.redisConfiguration.PoolSize; i++)
{
logger.LogDebug("Creating the redis connection pool with {0} connections.", this.redisConfiguration.PoolSize);
this.EmitConnection();
}
logger.LogDebug("Creating the redis connection pool with {0} connections.", this.redisConfiguration.PoolSize);
var tasks = Enumerable.Range(0, this.redisConfiguration.PoolSize)
.Select(_ => this.EmitConnection())
.ToArray();
Task.WaitAny(tasks); // wait for at least 1 connection to be available
}

/// <summary>
Expand Down