-
-
Notifications
You must be signed in to change notification settings - Fork 177
/
RedisConnectionPoolManager.cs
136 lines (111 loc) · 4.43 KB
/
RedisConnectionPoolManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) Ugo Lattanzi. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using StackExchange.Redis.Extensions.Core.Abstractions;
using StackExchange.Redis.Extensions.Core.Configuration;
using StackExchange.Redis.Extensions.Core.Extensions;
using StackExchange.Redis.Extensions.Core.Models;
namespace StackExchange.Redis.Extensions.Core.Implementations;
/// <inheritdoc/>
public sealed partial class RedisConnectionPoolManager : IRedisConnectionPoolManager
{
private static readonly object @lock = new();
private readonly IStateAwareConnection[] connections;
private readonly RedisConfiguration redisConfiguration;
private readonly ILogger<RedisConnectionPoolManager> logger;
private readonly Random random = new();
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="RedisConnectionPoolManager"/> class.
/// </summary>
/// <param name="redisConfiguration">The redis configuration.</param>
/// <param name="logger">The logger.</param>
public RedisConnectionPoolManager(RedisConfiguration redisConfiguration, ILogger<RedisConnectionPoolManager>? logger = null)
{
this.redisConfiguration = redisConfiguration ?? throw new ArgumentNullException(nameof(redisConfiguration));
this.logger = logger ?? NullLogger<RedisConnectionPoolManager>.Instance;
lock (@lock)
{
connections = new IStateAwareConnection[redisConfiguration.PoolSize];
EmitConnections();
}
}
/// <inheritdoc/>
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (isDisposed)
return;
if (disposing)
{
// free managed resources
foreach (var connection in connections)
connection.Dispose();
}
isDisposed = true;
}
/// <inheritdoc/>
public IConnectionMultiplexer GetConnection()
{
IStateAwareConnection connection;
switch (redisConfiguration.ConnectionSelectionStrategy)
{
case ConnectionSelectionStrategy.RoundRobin:
var nextIdx = random.Next(0, redisConfiguration.PoolSize);
connection = connections[nextIdx];
break;
case ConnectionSelectionStrategy.LeastLoaded:
connection = connections.MinBy(x => x.TotalOutstanding());
break;
default:
throw new InvalidEnumArgumentException(nameof(redisConfiguration.ConnectionSelectionStrategy), (int)redisConfiguration.ConnectionSelectionStrategy, typeof(ConnectionSelectionStrategy));
}
if (logger.IsEnabled(LogLevel.Debug))
logger.LogDebug("Using connection {HashCode} with {OutStanding} outstanding!", connection.Connection.GetHashCode().ToString(), connection.TotalOutstanding().ToString());
return connection.Connection;
}
/// <inheritdoc/>
public IEnumerable<IConnectionMultiplexer> GetConnections()
{
foreach (var connection in connections)
yield return connection.Connection;
}
/// <inheritdoc/>
public ConnectionPoolInformation GetConnectionInformation()
{
var activeConnections = 0;
var invalidConnections = 0;
foreach (var connection in connections)
{
if (!connection.IsConnected())
{
invalidConnections++;
continue;
}
activeConnections++;
}
return new()
{
RequiredPoolSize = redisConfiguration.PoolSize,
ActiveConnections = activeConnections,
InvalidConnections = invalidConnections
};
}
private void EmitConnections()
{
for (var i = 0; i < redisConfiguration.PoolSize; i++)
{
var multiplexer = ConnectionMultiplexer.Connect(redisConfiguration.ConfigurationOptions);
if (redisConfiguration.ProfilingSessionProvider != null)
multiplexer.RegisterProfiler(redisConfiguration.ProfilingSessionProvider);
connections[i] = redisConfiguration.StateAwareConnectionFactory(multiplexer, logger);
}
}
}