├── .github └── workflows │ ├── CODEOWNERS │ └── pr-pipeline.yml ├── .gitignore ├── CODEOWNERS.txt ├── LICENSE ├── README.md ├── StackExchange.Redis.MultiplexerPool.sln ├── samples └── RedisConnectionPoolConsoleApp │ ├── Program.cs │ └── RedisConnectionPoolConsoleApp.csproj ├── src └── StackExchange.Redis.MultiplexerPool │ ├── ConnectionMultiplexerPoolFactory.cs │ ├── ConnectionSelection │ ├── IConnectionSelector.cs │ ├── LoadBasedConnectionSelector.cs │ └── RoundRobinConnectionSelector.cs │ ├── ConnectionSelectionStrategy.cs │ ├── IConnectionMultiplexerPool.cs │ ├── IReconnectableConnectionMultiplexer.cs │ ├── Infra │ ├── Collections │ │ └── EnumerableExtensions.cs │ └── Common │ │ └── Guard.cs │ ├── MultiplexerPools │ └── ConnectionMultiplexerPool.cs │ ├── Multiplexers │ ├── ConnectionMultiplexerFactory.cs │ ├── IConnectionMultiplexerFactory.cs │ ├── IInternalDisposableConnectionMultiplexer.cs │ ├── IInternalReconnectableConnectionMultiplexer.cs │ ├── IReconnectableConnectionMultiplexerFactory.cs │ ├── InternalDisposableConnectionMultiplexer.cs │ ├── ReconnectableConnectionMultiplexer.cs │ └── ReconnectableConnectionMultiplexerFactory.cs │ ├── Properties │ └── AssemblyInfo.cs │ └── StackExchange.Redis.MultiplexerPool.csproj └── tests └── StackExchange.Redis.MultiplexerPool.Tests ├── ConnectionMultiplexerPoolFactoryTests.cs ├── ConnectionSelection └── RoundRobinConnectionSelectorTests.cs ├── Infra └── Collections │ └── EnumerableExtensionsTests.cs ├── MultiplexerPools └── ConnectionMultiplexerPoolTests.cs ├── Multiplexers ├── InternalDispoasbleConnectionMultiplexerTests.cs └── ReconnectableConnectionMultiplexerTests.cs └── StackExchange.Redis.MultiplexerPool.Tests.csproj /.github/workflows/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Code owners for the entire repository 2 | * @shabtay-matan 3 | -------------------------------------------------------------------------------- /.github/workflows/pr-pipeline.yml: -------------------------------------------------------------------------------- 1 | name: PR Pipeline 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | - develop 8 | 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - name: Checkout code 15 | uses: actions/checkout@v2 16 | 17 | - name: Set up .NET 18 | uses: actions/setup-dotnet@v2 19 | with: 20 | dotnet-version: '6.0.x' 21 | 22 | - name: Restore dependencies 23 | run: dotnet restore 24 | 25 | - name: Build 26 | run: dotnet build --no-restore 27 | 28 | - name: Run tests 29 | run: dotnet test --no-restore --verbosity normal 30 | 31 | - name: Run code linting 32 | run: dotnet format --verify-no-changes 33 | continue-on-error: true 34 | 35 | - name: Check code coverage 36 | run: dotnet test --collect:"XPlat Code Coverage" 37 | 38 | - name: Run security scan 39 | uses: github/codeql-action/init@v1 40 | with: 41 | languages: csharp 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | project.fragment.lock.json 46 | artifacts/ 47 | 48 | *_i.c 49 | *_p.c 50 | *_i.h 51 | *.ilk 52 | *.meta 53 | *.obj 54 | *.pch 55 | *.pdb 56 | *.pgc 57 | *.pgd 58 | *.rsp 59 | *.sbr 60 | *.tlb 61 | *.tli 62 | *.tlh 63 | *.tmp 64 | *.tmp_proj 65 | *.log 66 | *.vspscc 67 | *.vssscc 68 | .builds 69 | *.pidb 70 | *.svclog 71 | *.scc 72 | 73 | # Chutzpah Test files 74 | _Chutzpah* 75 | 76 | # Visual C++ cache files 77 | ipch/ 78 | *.aps 79 | *.ncb 80 | *.opendb 81 | *.opensdf 82 | *.sdf 83 | *.cachefile 84 | *.VC.db 85 | *.VC.VC.opendb 86 | 87 | # Visual Studio profiler 88 | *.psess 89 | *.vsp 90 | *.vspx 91 | *.sap 92 | 93 | # TFS 2012 Local Workspace 94 | $tf/ 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | *.DotSettings.user 103 | 104 | # JustCode is a .NET coding add-in 105 | .JustCode 106 | 107 | # TeamCity is a build add-in 108 | _TeamCity* 109 | 110 | # DotCover is a Code Coverage Tool 111 | *.dotCover 112 | 113 | # NCrunch 114 | _NCrunch_* 115 | .*crunch*.local.xml 116 | nCrunchTemp_* 117 | 118 | # MightyMoose 119 | *.mm.* 120 | AutoTest.Net/ 121 | 122 | # Web workbench (sass) 123 | .sass-cache/ 124 | 125 | # Installshield output folder 126 | [Ee]xpress/ 127 | 128 | # DocProject is a documentation generator add-in 129 | DocProject/buildhelp/ 130 | DocProject/Help/*.HxT 131 | DocProject/Help/*.HxC 132 | DocProject/Help/*.hhc 133 | DocProject/Help/*.hhk 134 | DocProject/Help/*.hhp 135 | DocProject/Help/Html2 136 | DocProject/Help/html 137 | 138 | # Click-Once directory 139 | publish/ 140 | 141 | # Publish Web Output 142 | *.[Pp]ublish.xml 143 | *.azurePubxml 144 | # TODO: Comment the next line if you want to checkin your web deploy settings 145 | # but database connection strings (with potential passwords) will be unencrypted 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 150 | # checkin your Azure Web App publish settings, but sensitive information contained 151 | # in these scripts will be unencrypted 152 | PublishScripts/ 153 | 154 | # NuGet Packages 155 | *.nupkg 156 | # The packages folder can be ignored because of Package Restore 157 | **/packages/* 158 | # except build/, which is used as an MSBuild target. 159 | !**/packages/build/ 160 | # Uncomment if necessary however generally it will be regenerated when needed 161 | #!**/packages/repositories.config 162 | # NuGet v3's project.json files produces more ignoreable files 163 | *.nuget.props 164 | *.nuget.targets 165 | 166 | # Microsoft Azure Build Output 167 | csx/ 168 | *.build.csdef 169 | 170 | # Microsoft Azure Emulator 171 | ecf/ 172 | rcf/ 173 | 174 | # Windows Store app package directories and files 175 | AppPackages/ 176 | BundleArtifacts/ 177 | Package.StoreAssociation.xml 178 | _pkginfo.txt 179 | 180 | # Visual Studio cache files 181 | # files ending in .cache can be ignored 182 | *.[Cc]ache 183 | # but keep track of directories ending in .cache 184 | !*.[Cc]ache/ 185 | 186 | # Others 187 | ClientBin/ 188 | ~$* 189 | *~ 190 | *.dbmdl 191 | *.dbproj.schemaview 192 | *.jfm 193 | *.pfx 194 | *.publishsettings 195 | node_modules/ 196 | orleans.codegen.cs 197 | 198 | # Since there are multiple workflows, uncomment next line to ignore bower_components 199 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 200 | #bower_components/ 201 | 202 | # RIA/Silverlight projects 203 | Generated_Code/ 204 | 205 | # Backup & report files from converting an old project file 206 | # to a newer Visual Studio version. Backup files are not needed, 207 | # because we have git ;-) 208 | _UpgradeReport_Files/ 209 | Backup*/ 210 | UpgradeLog*.XML 211 | UpgradeLog*.htm 212 | 213 | # SQL Server files 214 | *.mdf 215 | *.ldf 216 | 217 | # Business Intelligence projects 218 | *.rdl.data 219 | *.bim.layout 220 | *.bim_*.settings 221 | 222 | # Microsoft Fakes 223 | FakesAssemblies/ 224 | 225 | # GhostDoc plugin setting file 226 | *.GhostDoc.xml 227 | 228 | # Node.js Tools for Visual Studio 229 | .ntvs_analysis.dat 230 | 231 | # Visual Studio 6 build log 232 | *.plg 233 | 234 | # Visual Studio 6 workspace options file 235 | *.opt 236 | 237 | # Visual Studio LightSwitch build output 238 | **/*.HTMLClient/GeneratedArtifacts 239 | **/*.DesktopClient/GeneratedArtifacts 240 | **/*.DesktopClient/ModelManifest.xml 241 | **/*.Server/GeneratedArtifacts 242 | **/*.Server/ModelManifest.xml 243 | _Pvt_Extensions 244 | 245 | # Paket dependency manager 246 | .paket/paket.exe 247 | paket-files/ 248 | 249 | # FAKE - F# Make 250 | .fake/ 251 | 252 | # JetBrains Rider 253 | .idea/ 254 | *.sln.iml 255 | 256 | # CodeRush 257 | .cr/ 258 | 259 | # Python Tools for Visual Studio (PTVS) 260 | __pycache__/ 261 | *.pyc -------------------------------------------------------------------------------- /CODEOWNERS.txt: -------------------------------------------------------------------------------- 1 | # Code owners for the entire repository 2 | * @shabtay-matan 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 mataness 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # StackExchange.Redis.MultiplexerPool 2 | An extension library for StackExchange.Redis which adds Multiplexer connection pool abstraction and implementation. 3 | Refer the samples section for getting started. 4 | -------------------------------------------------------------------------------- /StackExchange.Redis.MultiplexerPool.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2020 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackExchange.Redis.MultiplexerPool", "src\StackExchange.Redis.MultiplexerPool\StackExchange.Redis.MultiplexerPool.csproj", "{F0921BB1-D64D-459E-9112-8995AC2663DC}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StackExchange.Redis.MultiplexerPool.Tests", "tests\StackExchange.Redis.MultiplexerPool.Tests\StackExchange.Redis.MultiplexerPool.Tests.csproj", "{978E8B5E-8F38-4DCC-95D2-28407CB15782}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedisConnectionPoolConsoleApp", "samples\RedisConnectionPoolConsoleApp\RedisConnectionPoolConsoleApp.csproj", "{C6D97262-7B56-42BC-B995-5A451CA5B9A0}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {F0921BB1-D64D-459E-9112-8995AC2663DC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {F0921BB1-D64D-459E-9112-8995AC2663DC}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {F0921BB1-D64D-459E-9112-8995AC2663DC}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {F0921BB1-D64D-459E-9112-8995AC2663DC}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {978E8B5E-8F38-4DCC-95D2-28407CB15782}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {978E8B5E-8F38-4DCC-95D2-28407CB15782}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {978E8B5E-8F38-4DCC-95D2-28407CB15782}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {978E8B5E-8F38-4DCC-95D2-28407CB15782}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {C6D97262-7B56-42BC-B995-5A451CA5B9A0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {C6D97262-7B56-42BC-B995-5A451CA5B9A0}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {C6D97262-7B56-42BC-B995-5A451CA5B9A0}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {C6D97262-7B56-42BC-B995-5A451CA5B9A0}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {B6405643-C0A8-4558-A807-F822F29ABDCA} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /samples/RedisConnectionPoolConsoleApp/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using StackExchange.Redis; 4 | using StackExchange.Redis.MultiplexerPool; 5 | 6 | namespace RedisConnectionPoolConsoleApp 7 | { 8 | class Program 9 | { 10 | static void Main(string[] args) 11 | { 12 | RunExampleAsync().Wait(); 13 | } 14 | 15 | public static async Task RunExampleAsync() 16 | { 17 | const string cRedisConnectionConfiguration = "REDIS_CONNECTION_CONFIG"; 18 | var poolSize = 10; 19 | 20 | _connectionPool = ConnectionMultiplexerPoolFactory.Create( 21 | poolSize: poolSize, 22 | configuration: cRedisConnectionConfiguration, 23 | connectionSelectionStrategy: ConnectionSelectionStrategy.RoundRobin); 24 | 25 | _connectionsErrorCount = new int[poolSize]; 26 | 27 | for (var i = 0; i < 100; i++) 28 | { 29 | var key = $"KEY_{i}"; 30 | var value = $"KEY_{i}"; 31 | await QueryRedisAsync(async db => await db.StringSetAsync(key, value)); 32 | } 33 | 34 | for (var i = 0; i < 100; i++) 35 | { 36 | var key = $"KEY_{i}"; 37 | var value = await QueryRedisAsync(async db => await db.StringGetAsync(key)); 38 | 39 | Console.WriteLine($"Key: '{key}' Value: '{value}'"); 40 | } 41 | } 42 | 43 | private static async Task QueryRedisAsync(Func> op) 44 | { 45 | var connection = await _connectionPool.GetAsync(); 46 | 47 | Console.WriteLine($"Connection '{connection.ConnectionIndex}' established at {connection.ConnectionTimeUtc}"); 48 | 49 | try 50 | { 51 | return await op(connection.Connection.GetDatabase()); 52 | } 53 | catch (RedisConnectionException) 54 | { 55 | _connectionsErrorCount[connection.ConnectionIndex]++; 56 | 57 | if (_connectionsErrorCount[connection.ConnectionIndex] < 3) 58 | { 59 | throw; 60 | } 61 | 62 | // Decide when to reconnect based on your own custom logic 63 | Console.WriteLine($"Re-establishing connection on index '{connection.ConnectionIndex}'"); 64 | 65 | await connection.ReconnectAsync(); 66 | 67 | return await op(connection.Connection.GetDatabase()); 68 | } 69 | } 70 | 71 | private static IConnectionMultiplexerPool _connectionPool; 72 | private static int[] _connectionsErrorCount; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /samples/RedisConnectionPoolConsoleApp/RedisConnectionPoolConsoleApp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/ConnectionMultiplexerPoolFactory.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | using System.IO; 3 | using StackExchange.Redis.MultiplexerPool.ConnectionSelection; 4 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 5 | using StackExchange.Redis.MultiplexerPool.MultiplexerPools; 6 | using StackExchange.Redis.MultiplexerPool.Multiplexers; 7 | 8 | namespace StackExchange.Redis.MultiplexerPool 9 | { 10 | /// 11 | /// A factory for . 12 | /// This factory acts as the entry point for clients of this library and it should be used for creating a connection pool of 13 | /// 14 | public static class ConnectionMultiplexerPoolFactory 15 | { 16 | /// 17 | /// Creates a new . 18 | /// Connections to the Redis server are lazily established, this method doesn't perform any I/O operation such as network call. 19 | /// For more info refer to interface docs 20 | /// 21 | /// The size of the connection pool 22 | /// The Redis connection string to use for establishing connections to the Redis server 23 | /// A that will be use to write logs created by the 24 | /// The connection selection strategy to be used by the connection pool 25 | /// The created connection pool 26 | /// is thread safe, for more info refer to the interface docs 27 | public static IConnectionMultiplexerPool Create( 28 | int poolSize, 29 | string configuration, 30 | TextWriter textWriter = null, 31 | ConnectionSelectionStrategy connectionSelectionStrategy = ConnectionSelectionStrategy.LeastLoaded) 32 | { 33 | Guard.CheckArgumentLowerBound(poolSize, 0, nameof(poolSize)); 34 | Guard.CheckNullArgument(configuration, nameof(configuration)); 35 | 36 | var connectionFactory = new ConnectionMultiplexerFactory(configuration, textWriter); 37 | 38 | return CreateInternal(poolSize, connectionFactory, connectionSelectionStrategy); 39 | } 40 | 41 | /// 42 | /// Creates a new . 43 | /// Connections to the Redis server are lazily established, this method doesn't perform any I/O operation such as network call. 44 | /// For more info refer to interface docs 45 | /// 46 | /// The size of the connection pool 47 | /// The Redis connection configuration to use for establishing connections to the Redis server 48 | /// A that will be use to write logs created by the 49 | /// The connection selection strategy to be used by the connection pool 50 | /// The created connection pool 51 | /// is thread safe, for more info refer to the interface docs 52 | public static IConnectionMultiplexerPool Create( 53 | int poolSize, 54 | ConfigurationOptions configurationOptions, 55 | TextWriter textWriter = null, 56 | ConnectionSelectionStrategy connectionSelectionStrategy = ConnectionSelectionStrategy.RoundRobin) 57 | { 58 | Guard.CheckArgumentLowerBound(poolSize, 0, nameof(poolSize)); 59 | Guard.CheckNullArgument(configurationOptions, nameof(connectionSelectionStrategy)); 60 | 61 | var connectionFactory = new ConnectionMultiplexerFactory(configurationOptions, textWriter); 62 | 63 | return CreateInternal(poolSize, connectionFactory, connectionSelectionStrategy); 64 | } 65 | 66 | private static IConnectionMultiplexerPool CreateInternal( 67 | int poolSize, 68 | IConnectionMultiplexerFactory connectionFactory, 69 | ConnectionSelectionStrategy connectionSelectionStrategy) 70 | { 71 | IConnectionSelector strategy; 72 | 73 | switch (connectionSelectionStrategy) 74 | { 75 | case ConnectionSelectionStrategy.RoundRobin: 76 | strategy = new RoundRobinConnectionSelector(); 77 | break; 78 | 79 | case ConnectionSelectionStrategy.LeastLoaded: 80 | strategy = new LoadBasedConnectionSelector(); 81 | break; 82 | 83 | default: 84 | throw new InvalidEnumArgumentException(nameof(connectionSelectionStrategy), (int)connectionSelectionStrategy, typeof(ConnectionSelectionStrategy)); 85 | } 86 | 87 | return new ConnectionMultiplexerPool(poolSize, new ReconnectableConnectionMultiplexerFactory(connectionFactory), strategy); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/ConnectionSelection/IConnectionSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace StackExchange.Redis.MultiplexerPool.ConnectionSelection 4 | { 5 | /// 6 | /// A contract for selecting a from a given collection of established connections. 7 | /// Each implementation defines its own strategy for selecting a connection. 8 | /// The implementation must be thread safe 9 | /// 10 | internal interface IConnectionSelector 11 | { 12 | /// 13 | /// Selects a connection from the given list and returns it 14 | /// 15 | /// The list of connections to choose from 16 | /// The selected connection 17 | IReconnectableConnectionMultiplexer Select(IReadOnlyList establishedConnections); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/ConnectionSelection/LoadBasedConnectionSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using StackExchange.Redis.MultiplexerPool.Infra.Collections; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool.ConnectionSelection 5 | { 6 | /// 7 | /// Implements . 8 | /// The implementation selects the connection by its amount of outstanding operations. 9 | /// The connection with the minimal outstanding connections is selected. 10 | /// The amount of outstanding operations of every connection is retrieved using the method. 11 | /// 12 | internal class LoadBasedConnectionSelector : IConnectionSelector 13 | { 14 | /// 15 | public IReconnectableConnectionMultiplexer Select(IReadOnlyList establishedConnections) 16 | => establishedConnections.MinBy(connection => connection.Connection.GetCounters().TotalOutstanding); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/ConnectionSelection/RoundRobinConnectionSelector.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool.ConnectionSelection 5 | { 6 | /// 7 | /// Implements . 8 | /// The implementation selects the connection in a round robin strategy in a thread safe manner. 9 | /// 10 | public class RoundRobinConnectionSelector : IConnectionSelector 11 | { 12 | internal RoundRobinConnectionSelector() 13 | { 14 | // Starting from -1 since we always get the next index in Get 15 | _lastConnectionChosen = -1; 16 | } 17 | 18 | /// 19 | public IReconnectableConnectionMultiplexer Select(IReadOnlyList establishedConnections) 20 | { 21 | var nextNumber = unchecked((uint)Interlocked.Increment(ref _lastConnectionChosen)); 22 | var nextIdx = (int)(nextNumber % establishedConnections.Count); 23 | 24 | return establishedConnections[nextIdx]; 25 | } 26 | 27 | private int _lastConnectionChosen; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/ConnectionSelectionStrategy.cs: -------------------------------------------------------------------------------- 1 | namespace StackExchange.Redis.MultiplexerPool 2 | { 3 | /// 4 | /// Possible strategies for selecting the returned for every call to 5 | /// 6 | public enum ConnectionSelectionStrategy 7 | { 8 | /// 9 | /// Every call to will return the next connection in the pool in a round robin manner. 10 | /// 11 | RoundRobin, 12 | 13 | /// 14 | /// Every call to will return the least loaded . 15 | /// The load of every connection is defined by it's . 16 | /// For more info refer to https://github.com/StackExchange/StackExchange.Redis/issues/512 . 17 | /// 18 | LeastLoaded 19 | } 20 | } -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/IConnectionMultiplexerPool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool 5 | { 6 | /// 7 | /// Represents a Redis pool. 8 | /// All the connections within the pool share the same Redis server configuration. 9 | /// Each implementation has it's own strategy to return the next connection from the pool (e.g. using round robin strategy). 10 | /// The implementations are expected to implement in a thread safe manner and optimized for performance. 11 | /// The implementation manages the connections to the Redis server and their lifetime. 12 | /// 13 | /// Some implementations may lazily establish a connection to the Redis server (which means that at some point of time, the number of connections 14 | /// established to the Redis server may be lower than . 15 | /// For that reason, is asynchronous. 16 | /// 17 | /// 18 | public interface IConnectionMultiplexerPool : IDisposable 19 | { 20 | /// 21 | /// Gets the size of the pool 22 | /// 23 | int PoolSize { get; } 24 | 25 | /// 26 | /// Gets a from the pool. 27 | /// 28 | /// 29 | Task GetAsync(); 30 | 31 | /// 32 | /// Closes all established connections in the pool. 33 | /// 34 | /// Whether to allow in-queue commands to complete first. 35 | /// A 36 | Task CloseAllAsync(bool allowCommandsToComplete = true); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/IReconnectableConnectionMultiplexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool 5 | { 6 | /// 7 | /// Wraps a and adds functionality to reconnect an existing connection to the Redis server. 8 | /// This functionality is useful in cases where the established connection is no longer stable. 9 | /// This has been reported by users such as here https://github.com/StackExchange/StackExchange.Redis/issues/1120 10 | /// 11 | public interface IReconnectableConnectionMultiplexer 12 | { 13 | /// 14 | /// Gets the that is already connected to the Redis server 15 | /// 16 | IConnectionMultiplexer Connection { get; } 17 | 18 | /// 19 | /// Gets the index of the connection (a value a value between 0 to PoolSize - 1) 20 | /// 21 | int ConnectionIndex { get; } 22 | 23 | /// 24 | /// Gets the time that the wrapped connection was established or reconnected 25 | /// 26 | DateTime ConnectionTimeUtc { get; } 27 | 28 | /// 29 | /// Creates a new wrapped . 30 | /// First, a new connection is being established and only after that, the previous connection is closed. 31 | /// Calls to from other threads will result in returning the previous connection until a new connection has been established. 32 | /// Concurrent calls to this method will be ignored and only the first call will be respected in order to avoid unwanted multiple reconnectios. 33 | /// 34 | /// Whether to allow in-queue commands to complete first. 35 | /// Whether to wait for the previous connection to close before returning to the caller 36 | /// A 37 | /// 38 | /// will never return a that was already disposed 39 | /// (even when is called from other threads when this method is being executed) 40 | /// 41 | Task ReconnectAsync(bool allowCommandsToComplete = true, bool fireAndForgetOnClose = true); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Infra/Collections/EnumerableExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 4 | 5 | namespace StackExchange.Redis.MultiplexerPool.Infra.Collections 6 | { 7 | /// 8 | /// Extension methods for 9 | /// 10 | internal static class EnumerableExtensions 11 | { 12 | /// 13 | /// Gets the minimal element in by comparing according to a computed value with default value comparer. 14 | /// 15 | /// 16 | /// The source collection. 17 | /// 18 | /// 19 | /// A selector that calculates a value for an element in that the comparison should be done by. 20 | /// Will receive null arguments if there are nulls in . 21 | /// 22 | /// The comparer to use for comparing between the 23 | /// 24 | /// Type of elements in . 25 | /// 26 | /// 27 | /// Type of value to compare elements by. 28 | /// 29 | /// 30 | /// The minimal element in . 31 | /// 32 | public static TSource MinBy( 33 | this IEnumerable source, 34 | Func selector, 35 | IComparer comparer = null) 36 | { 37 | Guard.CheckNullArgument(source, nameof(source)); 38 | Guard.CheckNullArgument(selector, nameof(selector)); 39 | 40 | 41 | comparer = comparer ?? Comparer.Default; 42 | 43 | using (var sourceIterator = source.GetEnumerator()) 44 | { 45 | if (!sourceIterator.MoveNext()) 46 | { 47 | throw new InvalidOperationException("Sequence contains no elements"); 48 | } 49 | var min = sourceIterator.Current; 50 | var minKey = selector(min); 51 | while (sourceIterator.MoveNext()) 52 | { 53 | var candidate = sourceIterator.Current; 54 | var candidateProjected = selector(candidate); 55 | if (comparer.Compare(candidateProjected, minKey) < 0) 56 | { 57 | min = candidate; 58 | minKey = candidateProjected; 59 | } 60 | } 61 | return min; 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Infra/Common/Guard.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool.Infra.Common 5 | { 6 | /// 7 | /// Helper class for checking preconditions and throw exceptions if those preconditions aren't met 8 | /// 9 | internal static class Guard 10 | { 11 | /// 12 | /// Throws an exception if is true 13 | /// 14 | /// 15 | /// Indication whether to throw the exception or not, this should be the result of an invalid argument check the caller does 16 | /// 17 | /// 18 | /// A messages that describes what's invalid about the argument(s) 19 | /// 20 | /// 21 | /// Thrown if is true 22 | /// 23 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 24 | public static void CheckArgument(bool shouldThrow, string message) 25 | { 26 | if (shouldThrow) 27 | { 28 | throw new ArgumentException(message); 29 | } 30 | } 31 | 32 | /// 33 | /// Throws an exception if is true 34 | /// 35 | /// 36 | /// Indication whether to throw the exception or not, this should be the result of an invalid argument check the caller does 37 | /// 38 | /// 39 | /// The name of the method parameter(s) checked. 40 | /// 41 | /// 42 | /// A messages that describes what's invalid about the argument(s) 43 | /// 44 | /// 45 | /// Thrown if is true 46 | /// 47 | public static void CheckArgument(bool shouldThrow, string paramName, string message) 48 | { 49 | if (shouldThrow) 50 | { 51 | throw new ArgumentException(message, paramName); 52 | } 53 | } 54 | /// 55 | /// Checks if is null and throws exception if it is, otherwise returning it 56 | /// 57 | /// 58 | /// The value to check if it's null 59 | /// 60 | /// 61 | /// The name of the method parameter checked. 62 | /// 63 | /// 64 | /// Type of argument checked. Used instead of accepting to ensure in compile-time 65 | /// the validation can be applied to ref types only, and not by accident applies to value types which will cause boxing 66 | /// 67 | /// 68 | /// If isn't null it is returned, otherwise an exception is thrown 69 | /// 70 | /// 71 | /// The exception thrown in case is null 72 | /// 73 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 74 | public static T CheckNullArgument(T argument, string paramName) 75 | where T : class 76 | { 77 | if (argument == null) 78 | { 79 | throw new ArgumentNullException(paramName); 80 | } 81 | 82 | return argument; 83 | } 84 | 85 | /// 86 | /// Checks whether an integer argument is greater/equal to a given min value 87 | /// 88 | /// 89 | /// The argument to check. 90 | /// 91 | /// 92 | /// Minimal allowed value for the argument, inclusive (meaning argument should be greater/equal than this value). 93 | /// 94 | /// 95 | /// The name of the method parameter checked. 96 | /// 97 | /// 98 | /// The argument, if it's greater/equal than the min value, otherwise an exception is thrown 99 | /// 100 | /// 101 | /// The exception thrown in case is not greater/equal than min value 102 | /// 103 | [MethodImpl(MethodImplOptions.AggressiveInlining)] 104 | public static int CheckArgumentLowerBound(int argument, int minValueInclusive, string paramName) 105 | { 106 | if (argument >= minValueInclusive) 107 | { 108 | return argument; 109 | } 110 | 111 | throw new ArgumentOutOfRangeException(paramName, argument, $"Argument should be in greater/equal than {minValueInclusive}"); 112 | } 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/MultiplexerPools/ConnectionMultiplexerPool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Nito.AsyncEx; 6 | using StackExchange.Redis.MultiplexerPool.ConnectionSelection; 7 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 8 | using StackExchange.Redis.MultiplexerPool.Multiplexers; 9 | 10 | namespace StackExchange.Redis.MultiplexerPool.MultiplexerPools 11 | { 12 | /// 13 | /// Implements . 14 | /// The implementation selects the connection to return from using the given 15 | /// The connections are lazily established. 16 | /// The amount of established connections will be equal to after calls to 17 | /// . 18 | /// 19 | internal class ConnectionMultiplexerPool : IConnectionMultiplexerPool 20 | { 21 | internal ConnectionMultiplexerPool( 22 | int poolSize, 23 | IReconnectableConnectionMultiplexerFactory connectionFactory, 24 | IConnectionSelector connectionSelector) 25 | { 26 | PoolSize = Guard.CheckArgumentLowerBound(poolSize, 1, nameof(poolSize)); 27 | Guard.CheckNullArgument(connectionFactory, nameof(connectionFactory)); 28 | Guard.CheckNullArgument(connectionSelector, nameof(connectionSelector)); 29 | 30 | _establishedConnections = new ConcurrentQueue(); 31 | _connectionFactory = connectionFactory; 32 | _connectionCreationLock = new AsyncLock(); 33 | _connectionSelector = connectionSelector; 34 | } 35 | /// 36 | public int PoolSize { get; } 37 | 38 | /// 39 | public async Task GetAsync() 40 | { 41 | if (_disposed) 42 | { 43 | throw new ObjectDisposedException(nameof(ConnectionMultiplexerPool)); 44 | } 45 | 46 | if (_establishedConnectionsAsArr == null) 47 | { 48 | using (await _connectionCreationLock.LockAsync().ConfigureAwait(false)) 49 | { 50 | if (_establishedConnectionsAsArr == null) 51 | { 52 | var connection = await _connectionFactory.CreateAsync(_establishedConnections.Count).ConfigureAwait(false); 53 | 54 | _establishedConnections.Enqueue(connection); 55 | 56 | if (_establishedConnections.Count == PoolSize) 57 | { 58 | _establishedConnectionsAsArr = _establishedConnections.ToArray(); 59 | } 60 | 61 | return connection; 62 | } 63 | } 64 | } 65 | 66 | return _connectionSelector.Select(_establishedConnectionsAsArr); 67 | } 68 | 69 | /// 70 | public Task CloseAllAsync(bool allowCommandsToComplete = true) 71 | { 72 | if (_disposed) 73 | { 74 | return Task.CompletedTask; 75 | } 76 | 77 | _disposed = true; 78 | 79 | return Task.WhenAll(_establishedConnections.Select(connection => connection.Connection.SafeCloseAsync(allowCommandsToComplete))); 80 | } 81 | 82 | /// 83 | public void Dispose() 84 | { 85 | if (_disposed) 86 | { 87 | return; 88 | } 89 | 90 | _disposed = true; 91 | 92 | foreach (var connectionToDispose in _establishedConnections) 93 | { 94 | connectionToDispose.Connection.SafeClose(); 95 | } 96 | } 97 | 98 | private readonly IConnectionSelector _connectionSelector; 99 | private readonly ConcurrentQueue _establishedConnections; 100 | private readonly AsyncLock _connectionCreationLock; 101 | private readonly IReconnectableConnectionMultiplexerFactory _connectionFactory; 102 | private IInternalReconnectableConnectionMultiplexer[] _establishedConnectionsAsArr; 103 | private bool _disposed; 104 | 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/ConnectionMultiplexerFactory.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using System.Threading.Tasks; 3 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 4 | 5 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 6 | { 7 | /// 8 | /// Implements . 9 | /// Creates instances of . 10 | /// The implementation establishes a connection to the Redis server using ConnectAsync methods. 11 | /// 12 | internal class ConnectionMultiplexerFactory : IConnectionMultiplexerFactory 13 | { 14 | /// 15 | /// Initializes a new instance of the class. 16 | /// 17 | /// The Redis connection string to use for establishing connections to the Redis server 18 | /// A that will be use to write logs created by the 19 | public ConnectionMultiplexerFactory(string configuration, TextWriter textWriter = null) 20 | { 21 | _configuration = Guard.CheckNullArgument(configuration, nameof(configuration)); 22 | _textWriter = textWriter; 23 | } 24 | 25 | /// 26 | /// Initializes a new instance of the class. 27 | /// 28 | /// The Redis connection configuration to use for establishing connections to the Redis server 29 | /// A that will be use to write logs created by the 30 | public ConnectionMultiplexerFactory(ConfigurationOptions configurationOptions, TextWriter textWriter = null) 31 | { 32 | _configurationOptions = Guard.CheckNullArgument(configurationOptions, nameof(configurationOptions)); 33 | _textWriter = textWriter; 34 | } 35 | 36 | /// 37 | public async Task CreateAsync() 38 | { 39 | IConnectionMultiplexer connectionMultiplexer; 40 | 41 | if (_configuration != null) 42 | { 43 | connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(_configuration, _textWriter).ConfigureAwait(continueOnCapturedContext: false); 44 | } 45 | else 46 | { 47 | connectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(_configurationOptions, _textWriter).ConfigureAwait(continueOnCapturedContext: false); 48 | } 49 | 50 | return new InternalDisposableConnectionMultiplexer(connectionMultiplexer); 51 | } 52 | 53 | private readonly string _configuration; 54 | private readonly TextWriter _textWriter; 55 | private readonly ConfigurationOptions _configurationOptions; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/IConnectionMultiplexerFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 4 | { 5 | /// 6 | /// A factory for 7 | /// 8 | internal interface IConnectionMultiplexerFactory 9 | { 10 | /// 11 | /// Creates a new . 12 | /// The created instance is already connected to the Redis server. 13 | /// 14 | /// The created 15 | Task CreateAsync(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/IInternalDisposableConnectionMultiplexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 5 | { 6 | /// 7 | /// An extended contract of which adds methods that should be used internally for safely close the connection to 8 | /// the Redis server. 9 | /// A connection should be closed only when it's not a part of a connection pool. 10 | /// The traditional dispose methods defined in will be blocked by the implementation, resulting in thrown 11 | /// . 12 | /// 13 | internal interface IInternalDisposableConnectionMultiplexer : IConnectionMultiplexer 14 | { 15 | /// 16 | /// Closes the connection to the Redis server 17 | /// 18 | /// Whether to allow in-queue commands to complete first. 19 | /// A 20 | Task SafeCloseAsync(bool allowCommandsToComplete = true); 21 | 22 | /// 23 | /// Closes the connection to the Redis server 24 | /// 25 | /// Whether to allow in-queue commands to complete first. 26 | void SafeClose(bool allowCommandsToComplete = true); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/IInternalReconnectableConnectionMultiplexer.cs: -------------------------------------------------------------------------------- 1 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 2 | { 3 | /// 4 | /// An extended contract of which overrides the by returning 5 | /// . 6 | /// 7 | internal interface IInternalReconnectableConnectionMultiplexer : IReconnectableConnectionMultiplexer 8 | { 9 | new IInternalDisposableConnectionMultiplexer Connection { get; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/IReconnectableConnectionMultiplexerFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 4 | { 5 | /// 6 | /// A factory for 7 | /// 8 | internal interface IReconnectableConnectionMultiplexerFactory 9 | { 10 | /// 11 | /// Creates a new . 12 | /// The created instance is already connected to the Redis server. 13 | /// 14 | /// The created connection 15 | Task CreateAsync(int connectionIndex); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/InternalDisposableConnectionMultiplexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Net; 4 | using System.Threading.Tasks; 5 | using StackExchange.Redis.Maintenance; 6 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 7 | using StackExchange.Redis.Profiling; 8 | 9 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 10 | { 11 | /// 12 | /// Implements in a decorator style pattern by decorating a given . 13 | /// The implementation passes the calls to the decorated . 14 | /// Calls to , or will result in being thrown. 15 | /// 16 | internal class InternalDisposableConnectionMultiplexer : IInternalDisposableConnectionMultiplexer 17 | { 18 | public InternalDisposableConnectionMultiplexer(IConnectionMultiplexer wrappedConnectionMultiplexer) 19 | { 20 | _wrappedConnectionMultiplexer = Guard.CheckNullArgument(wrappedConnectionMultiplexer, nameof(wrappedConnectionMultiplexer)); 21 | } 22 | 23 | /// 24 | public void Dispose() 25 | => throw CreateDisposeNotAllowedException(); 26 | 27 | /// 28 | public ValueTask DisposeAsync() 29 | => new ValueTask(Task.FromException(CreateDisposeNotAllowedException())); 30 | 31 | /// 32 | public Task SafeCloseAsync(bool allowCommandsToComplete = true) 33 | => _wrappedConnectionMultiplexer.CloseAsync(allowCommandsToComplete); 34 | 35 | /// 36 | public void SafeClose(bool allowCommandsToComplete = true) 37 | => _wrappedConnectionMultiplexer.Close(); 38 | 39 | /// 40 | public void RegisterProfiler(Func profilingSessionProvider) 41 | => _wrappedConnectionMultiplexer.RegisterProfiler(profilingSessionProvider); 42 | 43 | /// 44 | public ServerCounters GetCounters() 45 | => _wrappedConnectionMultiplexer.GetCounters(); 46 | 47 | /// 48 | public EndPoint[] GetEndPoints(bool configuredOnly = false) 49 | => _wrappedConnectionMultiplexer.GetEndPoints(configuredOnly); 50 | 51 | /// 52 | public void Wait(Task task) 53 | => _wrappedConnectionMultiplexer.Wait(task); 54 | 55 | /// 56 | public T Wait(Task task) 57 | => _wrappedConnectionMultiplexer.Wait(task); 58 | 59 | /// 60 | public void WaitAll(params Task[] tasks) 61 | => _wrappedConnectionMultiplexer.WaitAll(tasks); 62 | 63 | /// 64 | public int HashSlot(RedisKey key) 65 | => _wrappedConnectionMultiplexer.HashSlot(key); 66 | 67 | /// 68 | public ISubscriber GetSubscriber(object asyncState = null) 69 | => _wrappedConnectionMultiplexer.GetSubscriber(asyncState); 70 | 71 | 72 | /// 73 | public IDatabase GetDatabase(int db = -1, object asyncState = null) 74 | => _wrappedConnectionMultiplexer.GetDatabase(db, asyncState); 75 | 76 | 77 | /// 78 | public IServer GetServer(string host, int port, object asyncState = null) 79 | => _wrappedConnectionMultiplexer.GetServer(host, port, asyncState); 80 | 81 | 82 | /// 83 | public IServer GetServer(string hostAndPort, object asyncState = null) 84 | => _wrappedConnectionMultiplexer.GetServer(hostAndPort, asyncState); 85 | 86 | 87 | /// 88 | public IServer GetServer(IPAddress host, int port) 89 | => _wrappedConnectionMultiplexer.GetServer(host, port); 90 | 91 | 92 | /// 93 | public IServer GetServer(EndPoint endpoint, object asyncState = null) 94 | => _wrappedConnectionMultiplexer.GetServer(endpoint, asyncState); 95 | 96 | /// 97 | public IServer[] GetServers() 98 | => _wrappedConnectionMultiplexer.GetServers(); 99 | 100 | 101 | /// 102 | public Task ConfigureAsync(TextWriter log = null) 103 | => _wrappedConnectionMultiplexer.ConfigureAsync(log); 104 | 105 | 106 | /// 107 | public bool Configure(TextWriter log = null) 108 | => _wrappedConnectionMultiplexer.Configure(log); 109 | 110 | 111 | /// 112 | public string GetStatus() 113 | => _wrappedConnectionMultiplexer.GetStatus(); 114 | 115 | 116 | /// 117 | public void GetStatus(TextWriter log) 118 | => _wrappedConnectionMultiplexer.GetStatus(log); 119 | 120 | 121 | /// 122 | public void Close(bool allowCommandsToComplete = true) 123 | => throw CreateDisposeNotAllowedException(); 124 | 125 | 126 | /// 127 | public Task CloseAsync(bool allowCommandsToComplete = true) 128 | => throw CreateDisposeNotAllowedException(); 129 | 130 | 131 | /// 132 | public string GetStormLog() 133 | => _wrappedConnectionMultiplexer.GetStormLog(); 134 | 135 | 136 | /// 137 | public void ResetStormLog() 138 | => _wrappedConnectionMultiplexer.ResetStormLog(); 139 | 140 | 141 | /// 142 | public long PublishReconfigure(CommandFlags flags = CommandFlags.None) 143 | => _wrappedConnectionMultiplexer.PublishReconfigure(flags); 144 | 145 | 146 | /// 147 | public Task PublishReconfigureAsync(CommandFlags flags = CommandFlags.None) 148 | => _wrappedConnectionMultiplexer.PublishReconfigureAsync(flags); 149 | 150 | 151 | /// 152 | public int GetHashSlot(RedisKey key) 153 | => _wrappedConnectionMultiplexer.GetHashSlot(key); 154 | 155 | 156 | /// 157 | public void ExportConfiguration(Stream destination, ExportOptions options = ExportOptions.All) 158 | => _wrappedConnectionMultiplexer.ExportConfiguration(destination, options); 159 | 160 | public void AddLibraryNameSuffix(string suffix) 161 | => _wrappedConnectionMultiplexer.AddLibraryNameSuffix(suffix); 162 | 163 | 164 | /// 165 | public string ClientName => _wrappedConnectionMultiplexer.ClientName; 166 | 167 | /// 168 | public string Configuration => _wrappedConnectionMultiplexer.Configuration; 169 | 170 | /// 171 | public int TimeoutMilliseconds => _wrappedConnectionMultiplexer.TimeoutMilliseconds; 172 | 173 | /// 174 | public long OperationCount => _wrappedConnectionMultiplexer.OperationCount; 175 | 176 | /// 177 | public bool PreserveAsyncOrder 178 | { 179 | #pragma warning disable CS0618 // Type or member is obsolete 180 | get => _wrappedConnectionMultiplexer.PreserveAsyncOrder; 181 | 182 | set => _wrappedConnectionMultiplexer.PreserveAsyncOrder = value; 183 | #pragma warning restore CS0618 // Type or member is obsolete 184 | } 185 | 186 | /// 187 | public bool IsConnected => _wrappedConnectionMultiplexer.IsConnected; 188 | 189 | /// 190 | public bool IsConnecting => _wrappedConnectionMultiplexer.IsConnecting; 191 | 192 | /// 193 | [Obsolete("Obsolete")] 194 | public bool IncludeDetailInExceptions 195 | { 196 | get => _wrappedConnectionMultiplexer.IncludeDetailInExceptions; 197 | set => _wrappedConnectionMultiplexer.IncludeDetailInExceptions = value; 198 | } 199 | 200 | /// 201 | public int StormLogThreshold 202 | { 203 | get => _wrappedConnectionMultiplexer.StormLogThreshold; 204 | set => _wrappedConnectionMultiplexer.StormLogThreshold = value; 205 | } 206 | 207 | /// 208 | public event EventHandler ErrorMessage 209 | { 210 | add => _wrappedConnectionMultiplexer.ErrorMessage += value; 211 | remove => _wrappedConnectionMultiplexer.ErrorMessage -= value; 212 | } 213 | 214 | /// 215 | public event EventHandler ConnectionFailed 216 | { 217 | add => _wrappedConnectionMultiplexer.ConnectionFailed += value; 218 | remove => _wrappedConnectionMultiplexer.ConnectionFailed -= value; 219 | } 220 | 221 | /// 222 | public event EventHandler InternalError 223 | { 224 | add => _wrappedConnectionMultiplexer.InternalError += value; 225 | remove => _wrappedConnectionMultiplexer.InternalError -= value; 226 | } 227 | 228 | /// 229 | public event EventHandler ConnectionRestored 230 | { 231 | add => _wrappedConnectionMultiplexer.ConnectionRestored += value; 232 | remove => _wrappedConnectionMultiplexer.ConnectionRestored -= value; 233 | } 234 | 235 | /// 236 | public event EventHandler ConfigurationChanged 237 | { 238 | add => _wrappedConnectionMultiplexer.ConfigurationChanged += value; 239 | remove => _wrappedConnectionMultiplexer.ConfigurationChanged -= value; 240 | } 241 | 242 | /// 243 | public event EventHandler ConfigurationChangedBroadcast 244 | { 245 | add => _wrappedConnectionMultiplexer.ConfigurationChangedBroadcast += value; 246 | remove => _wrappedConnectionMultiplexer.ConfigurationChangedBroadcast -= value; 247 | } 248 | 249 | public event EventHandler ServerMaintenanceEvent 250 | { 251 | add => _wrappedConnectionMultiplexer.ServerMaintenanceEvent += value; 252 | remove => _wrappedConnectionMultiplexer.ServerMaintenanceEvent -= value; 253 | } 254 | 255 | /// 256 | public event EventHandler HashSlotMoved 257 | { 258 | add => _wrappedConnectionMultiplexer.HashSlotMoved += value; 259 | remove => _wrappedConnectionMultiplexer.HashSlotMoved -= value; 260 | } 261 | 262 | private static InvalidOperationException CreateDisposeNotAllowedException() 263 | => new InvalidOperationException($"Disposing or closing the connection of '{nameof(IConnectionMultiplexer)}' is not allowed, please dispose / close the '{nameof(IConnectionMultiplexerPool)}' instead"); 264 | 265 | private readonly IConnectionMultiplexer _wrappedConnectionMultiplexer; 266 | } 267 | } 268 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/ReconnectableConnectionMultiplexer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Nito.AsyncEx; 4 | using StackExchange.Redis.MultiplexerPool.Infra.Common; 5 | 6 | #pragma warning disable 4014 7 | 8 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 9 | { 10 | /// 11 | /// Implements . 12 | /// 13 | internal class ReconnectableConnectionMultiplexer : IInternalReconnectableConnectionMultiplexer 14 | { 15 | public ReconnectableConnectionMultiplexer( 16 | int connectionIndex, 17 | IInternalDisposableConnectionMultiplexer connectionMultiplexer, 18 | IConnectionMultiplexerFactory connectionMultiplexerFactory) 19 | { 20 | ConnectionIndex = Guard.CheckArgumentLowerBound(connectionIndex, 0, nameof(connectionIndex)); 21 | ConnectionTimeUtc = DateTime.UtcNow; 22 | _connectLock = new AsyncLock(); 23 | Connection = Guard.CheckNullArgument(connectionMultiplexer, nameof(connectionMultiplexer)); 24 | _connectionMultiplexerFactory = Guard.CheckNullArgument(connectionMultiplexerFactory, nameof(connectionMultiplexerFactory)); 25 | } 26 | 27 | /// 28 | IConnectionMultiplexer IReconnectableConnectionMultiplexer.Connection => Connection; 29 | 30 | /// 31 | public IInternalDisposableConnectionMultiplexer Connection { get; private set; } 32 | 33 | /// 34 | public int ConnectionIndex { get; } 35 | 36 | /// 37 | public DateTime ConnectionTimeUtc { get; private set; } 38 | 39 | /// 40 | public async Task ReconnectAsync(bool allowCommandsToComplete = true, bool fireAndForgetOnClose = true) 41 | { 42 | var connectionTimeUtc = ConnectionTimeUtc; 43 | using (await _connectLock.LockAsync().ConfigureAwait(false)) 44 | { 45 | if (connectionTimeUtc == ConnectionTimeUtc) 46 | { 47 | var previousConnection = Connection; 48 | 49 | Connection = await _connectionMultiplexerFactory.CreateAsync(); 50 | 51 | if (fireAndForgetOnClose) 52 | { 53 | previousConnection.SafeCloseAsync(allowCommandsToComplete); 54 | } 55 | else 56 | { 57 | await previousConnection.SafeCloseAsync(allowCommandsToComplete).ConfigureAwait(false); 58 | } 59 | 60 | ConnectionTimeUtc = DateTime.UtcNow; 61 | } 62 | } 63 | } 64 | 65 | private readonly IConnectionMultiplexerFactory _connectionMultiplexerFactory; 66 | private readonly AsyncLock _connectLock; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Multiplexers/ReconnectableConnectionMultiplexerFactory.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | 3 | namespace StackExchange.Redis.MultiplexerPool.Multiplexers 4 | { 5 | /// 6 | /// Implements . 7 | /// Creates instances of . 8 | /// 9 | internal class ReconnectableConnectionMultiplexerFactory : IReconnectableConnectionMultiplexerFactory 10 | { 11 | /// 12 | /// Initializes a new instance of the class. 13 | /// 14 | /// Will be used to create the wrapped by the 15 | public ReconnectableConnectionMultiplexerFactory(IConnectionMultiplexerFactory connectionMultiplexerFactory) 16 | { 17 | _connectionMultiplexerFactory = connectionMultiplexerFactory; 18 | } 19 | 20 | /// 21 | public async Task CreateAsync(int connectionIndex) 22 | { 23 | var connection = await _connectionMultiplexerFactory.CreateAsync().ConfigureAwait(false); 24 | 25 | return new ReconnectableConnectionMultiplexer(connectionIndex, connection, _connectionMultiplexerFactory); 26 | } 27 | 28 | private readonly IConnectionMultiplexerFactory _connectionMultiplexerFactory; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyCopyright("Copyright © 2019")] 9 | [assembly: AssemblyTrademark("")] 10 | [assembly: AssemblyCulture("")] 11 | [assembly: InternalsVisibleTo("StackExchange.Redis.MultiplexerPool.Tests")] 12 | [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")] 13 | 14 | // Setting ComVisible to false makes the types in this assembly not visible 15 | // to COM components. If you need to access a type in this assembly from 16 | // COM, set the ComVisible attribute to true on that type. 17 | [assembly: ComVisible(false)] 18 | 19 | // The following GUID is for the ID of the typelib if this project is exposed to COM 20 | [assembly: Guid("ec918a5a-1070-487c-aa2e-507522427e5b")] 21 | 22 | // Version information for an assembly consists of the following four values: 23 | // 24 | // Major Version 25 | // Minor Version 26 | // Build Number 27 | // Revision 28 | // 29 | -------------------------------------------------------------------------------- /src/StackExchange.Redis.MultiplexerPool/StackExchange.Redis.MultiplexerPool.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0;netstandard2.1 5 | false 6 | An extension library for StackExchange.Redis which adds Multiplexer connection pool abstraction and implementation 7 | Redis, StackExchange.Redis, Connection pool, Multiplexer 8 | https://github.com/mataness/StackExchange.Redis.MultiplexerPool 9 | https://github.com/mataness/StackExchange.Redis.MultiplexerPool 10 | MIT 11 | StackExchange.Redis.MultiplexerPool 12 | StackExchange.Redis.MultiplexerPool 13 | StackExchange.Redis.MultiplexerPool 14 | StackExchange.Redis.MultiplexerPool 15 | StackExchange.Redis.MultiplexerPool 16 | 2.2.0 17 | 18 | 2.2.0 19 | - Adding support for .net standard 2.0 20 | 2.1.0 21 | - Updating depdencies 22 | 2.0.1 23 | - Fixing GetServers 24 | 2.0.0 25 | - Moving to .NET Standard 2.1 and updating Redis package to 2.6.111 26 | 1.0.2 27 | - Bugfix: The ReconnectableConnectionMultiplexer.ConnectionTimeUtc wasn't updated on ReconnectAsync 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/ConnectionMultiplexerPoolFactoryTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using AutoFixture; 4 | using FluentAssertions; 5 | using NUnit.Framework; 6 | using StackExchange.Redis.MultiplexerPool.ConnectionSelection; 7 | using StackExchange.Redis.MultiplexerPool.MultiplexerPools; 8 | using TestStack.BDDfy; 9 | 10 | namespace StackExchange.Redis.MultiplexerPool.Tests 11 | { 12 | /// 13 | /// Test class for 14 | /// 15 | [TestFixture] 16 | public class ConnectionMultiplexerPoolFactoryTests 17 | { 18 | [SetUp] 19 | public void SetUp() 20 | { 21 | _fixture = new Fixture(); 22 | _fixture.Customizations.Add(new RandomNumericSequenceGenerator(1, 100)); 23 | _expectedSize = 0; 24 | _createdPool = null; 25 | } 26 | 27 | [Test] 28 | [TestCase(ConnectionSelectionStrategy.LeastLoaded, typeof(LoadBasedConnectionSelector))] 29 | [TestCase(ConnectionSelectionStrategy.RoundRobin, typeof(RoundRobinConnectionSelector))] 30 | public void Creates_a_connection_pool_based_on_the_given_strategy_and_with_the_given_size(ConnectionSelectionStrategy strategy, Type expectedConnectionSelectorType) 31 | { 32 | this.When(_ => Creating_a_connection_pool_with_strategy_and_size(strategy, _fixture.Create())) 33 | .Then(_ => The_type_of_the_created_connection_selector_is(expectedConnectionSelectorType)) 34 | .And(_ => The_size_of_the_pool_is(_expectedSize)) 35 | .BDDfy(); 36 | } 37 | 38 | private void Creating_a_connection_pool_with_strategy_and_size(ConnectionSelectionStrategy strategy, int poolSize) 39 | { 40 | _expectedSize = poolSize; 41 | 42 | /* Providing a random string which acts as a mock for the connection string, 43 | will not result in exception as the connections are lazily created */ 44 | _createdPool = ConnectionMultiplexerPoolFactory.Create( 45 | poolSize, 46 | _fixture.Create() , 47 | connectionSelectionStrategy: strategy) as ConnectionMultiplexerPool; 48 | } 49 | 50 | private void The_type_of_the_created_connection_selector_is(Type expectedConnectionPoolType) 51 | { 52 | 53 | BindingFlags bindFlags = BindingFlags.Instance | BindingFlags.NonPublic; 54 | 55 | FieldInfo field = _createdPool.GetType().GetField("_connectionSelector", bindFlags); 56 | 57 | field.GetValue(_createdPool).Should().BeOfType(expectedConnectionPoolType); 58 | } 59 | 60 | private void The_size_of_the_pool_is(int expectedSize) 61 | => AssertionExtensions.Should((int) _createdPool.PoolSize).Be(expectedSize); 62 | 63 | private Fixture _fixture; 64 | private ConnectionMultiplexerPool _createdPool; 65 | private int _expectedSize; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/ConnectionSelection/RoundRobinConnectionSelectorTests.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Linq; 3 | using FluentAssertions; 4 | using Moq; 5 | using NUnit.Framework; 6 | using StackExchange.Redis.MultiplexerPool.ConnectionSelection; 7 | using TestStack.BDDfy; 8 | 9 | namespace StackExchange.Redis.MultiplexerPool.Tests.ConnectionSelection 10 | { 11 | /// 12 | /// Test class for 13 | /// 14 | [TestFixture] 15 | public class RoundRobinConnectionSelectorTests 16 | { 17 | [SetUp] 18 | public void SetUp() 19 | { 20 | _roundRobinConnectionSelector = new RoundRobinConnectionSelector(); 21 | _connections = null; 22 | _selectedConnection = null; 23 | } 24 | 25 | [Test] 26 | public void Selects_the_connection_in_round_robin_strategy() 27 | { 28 | this.Given(_ => A_list_of_connections_in_size_of(3)) 29 | .When(_ => Selecting_a_connection()) 30 | .Then(_ => The_selected_connection_index_is(0)) 31 | .When(_ => Selecting_a_connection()) 32 | .Then(_ => The_selected_connection_index_is(1)) 33 | .When(_ => Selecting_a_connection()) 34 | .Then(_ => The_selected_connection_index_is(2)) 35 | .When(_ => Selecting_a_connection()) 36 | .Then(_ => The_selected_connection_index_is(0)) 37 | .BDDfy(); 38 | } 39 | 40 | [Test] 41 | public void Always_returns_the_same_connection_when_the_connection_list_is_in_size_of_one() 42 | { 43 | this.Given(_ => A_list_of_connections_in_size_of(1)) 44 | .When(_ => Selecting_a_connection()) 45 | .Then(_ => The_selected_connection_index_is(0)) 46 | .When(_ => Selecting_a_connection()) 47 | .Then(_ => The_selected_connection_index_is(0)) 48 | .When(_ => Selecting_a_connection()) 49 | .Then(_ => The_selected_connection_index_is(0)) 50 | .BDDfy(); 51 | } 52 | 53 | private void A_list_of_connections_in_size_of(int size) 54 | => _connections = Enumerable.Range(0, size) 55 | .Select(_ => new Mock().Object) 56 | .ToList(); 57 | 58 | private void Selecting_a_connection() 59 | => _selectedConnection = _roundRobinConnectionSelector.Select(_connections); 60 | 61 | private void The_selected_connection_index_is(int expectedIndex) 62 | => _selectedConnection.Should().BeSameAs(_connections[expectedIndex]); 63 | 64 | private List _connections; 65 | private IReconnectableConnectionMultiplexer _selectedConnection; 66 | private RoundRobinConnectionSelector _roundRobinConnectionSelector; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/Infra/Collections/EnumerableExtensionsTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using AutoFixture; 5 | using FluentAssertions; 6 | using NUnit.Framework; 7 | using TestStack.BDDfy; 8 | 9 | namespace StackExchange.Redis.MultiplexerPool.Tests.Infra.Collections 10 | { 11 | /// 12 | /// Test class for 13 | /// 14 | [TestFixture] 15 | public class EnumerableExtensionsTests 16 | { 17 | [SetUp] 18 | public void SetUp() 19 | { 20 | _returnedMinimum = null; 21 | _collection = null; 22 | _fixture = new Fixture(); 23 | } 24 | 25 | [Test] 26 | [TestCase(10)] 27 | [TestCase(1)] 28 | [TestCase(2)] 29 | public void Returns_the_item_with_the_minimum_value(int collectionSize) 30 | { 31 | this.Given(_ => A_collection_of_numbers_with_size(collectionSize)) 32 | .When(_ => Getting_the_minimum_number_in_the_collection()) 33 | .Then(_ => The_minimal_number_in_the_collection_was_returned()) 34 | .BDDfy(); 35 | } 36 | 37 | [Test] 38 | public void Throws_InvalidOperationException_when_the_collection_is_empty() 39 | { 40 | this.Given(_ => An_empty_collection_of_numbers()) 41 | .When(_ => Getting_the_minimum_number_in_the_collection()) 42 | .Then(_ => InvalidOperationException_was_thrown()) 43 | .BDDfy(); 44 | } 45 | 46 | private void An_empty_collection_of_numbers() 47 | => _collection = new List(); 48 | 49 | private void A_collection_of_numbers_with_size(int collectionSize) 50 | => _collection = _fixture.CreateMany(collectionSize).ToList(); 51 | 52 | private void Getting_the_minimum_number_in_the_collection() 53 | { 54 | try 55 | { 56 | _returnedMinimum = MultiplexerPool.Infra.Collections.EnumerableExtensions.MinBy(_collection, num => num); 57 | } 58 | catch (Exception ex) 59 | { 60 | _exceptionThrown = ex; 61 | } 62 | } 63 | 64 | private void The_minimal_number_in_the_collection_was_returned() 65 | => _returnedMinimum.Should().Be(_collection.Min()); 66 | 67 | private void InvalidOperationException_was_thrown() 68 | => _exceptionThrown.Should().BeOfType(); 69 | 70 | 71 | private List _collection; 72 | private int? _returnedMinimum; 73 | private Exception _exceptionThrown; 74 | private Fixture _fixture; 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/MultiplexerPools/ConnectionMultiplexerPoolTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | using FluentAssertions; 5 | using Moq; 6 | using NUnit.Framework; 7 | using StackExchange.Redis.MultiplexerPool.ConnectionSelection; 8 | using StackExchange.Redis.MultiplexerPool.MultiplexerPools; 9 | using StackExchange.Redis.MultiplexerPool.Multiplexers; 10 | using TestStack.BDDfy; 11 | 12 | namespace StackExchange.Redis.MultiplexerPool.Tests.MultiplexerPools 13 | { 14 | /// 15 | /// Test class for 16 | /// 17 | [TestFixture] 18 | public class ConnectionMultiplexerPoolTests 19 | { 20 | [SetUp] 21 | public void SetUp() 22 | { 23 | _connectionFactoryMock = null; 24 | _connectionPool = null; 25 | _exceptionThrown = null; 26 | _retrievedConnection = null; 27 | _connectionSelectorMock = null; 28 | _createdConnectionsMocks = new List>(); 29 | } 30 | 31 | [Test] 32 | public void Returns_the_connection_chosen_by_the_selector_when_all_connections_has_been_established() 33 | { 34 | this.Given(_ => A_connection_pool_of_size(1)) 35 | .When(_ => Getting_a_connection()) 36 | .When(_ => Getting_a_connection()) 37 | .Then(_ => The_return_connection_was_chosen_by_the_selector()) 38 | .BDDfy(); 39 | } 40 | 41 | [Test] 42 | public void Passing_the_allowCommandsToComplete_as_true_when_disposing() 43 | { 44 | this.Given(_ => A_connection_pool()) 45 | .When(_ => Disposing()) 46 | .Then(_ => All_open_connections_has_been_closed_with_allowCommandsToComplete_set_to(true)) 47 | .BDDfy(); 48 | } 49 | 50 | [Test] 51 | [TestCase(true)] 52 | [TestCase(false)] 53 | public void Passing_the_given_allowCommandsToComplete_when_closing_all(bool value) 54 | { 55 | this.Given(_ => A_connection_pool()) 56 | .When(_ => Closing_all_connections_with_allowCommandsToComplete_set_to(value)) 57 | .Then(_ => All_open_connections_has_been_closed_with_allowCommandsToComplete_set_to(value)) 58 | .BDDfy(); 59 | } 60 | 61 | [Test] 62 | [TestCase(1)] 63 | [TestCase(5)] 64 | public void Returns_the_pool_size(int poolSize) 65 | { 66 | this.Given(_ => A_connection_pool_of_size(poolSize)) 67 | .When(_ => Getting_the_pool_size()) 68 | .Then(_ => The_returned_pool_size_value_is(poolSize)) 69 | .BDDfy(); 70 | } 71 | 72 | [Test] 73 | public void Throws_object_disposed_exception_when_trying_to_get_a_connection_after_closing_all_connections() 74 | { 75 | this.Given(_ => A_connection_pool_of_size(10)) 76 | .When(_ => Closing_all_connections()) 77 | .And(_ => Getting_a_connection()) 78 | .Then(_ => ObjectDisposedException_was_thrown()) 79 | .BDDfy(); 80 | } 81 | 82 | [Test] 83 | public void Throws_object_disposed_exception_when_trying_to_get_a_connection_after_disposing() 84 | { 85 | this.Given(_ => A_connection_pool_of_size(10)) 86 | .When(_ => Disposing_the_pool()) 87 | .And(_ => Getting_a_connection()) 88 | .Then(_ => ObjectDisposedException_was_thrown()) 89 | .BDDfy(); 90 | } 91 | 92 | [Test] 93 | [TestCase(1)] 94 | [TestCase(5)] 95 | public void Creates_a_new_connection_when_pool_size_hasnt_reached(int poolSize) 96 | { 97 | this.Given(_ => A_connection_pool_of_size(poolSize)) 98 | .When(_ => Getting_a_connection()) 99 | .Then(_ => The_amount_of_created_connections_is(1)) 100 | .When(_ => Getting_a_connection()) 101 | .Then(_ => The_amount_of_created_connections_is(Math.Min(poolSize, 2))) 102 | .BDDfy(); 103 | } 104 | 105 | private void A_connection_pool() 106 | => A_connection_pool_of_size(10); 107 | 108 | private void Disposing() => _connectionPool.Dispose(); 109 | 110 | private void A_connection_pool_of_size(int size) 111 | { 112 | _connectionFactoryMock = CreateConnectionFactoryMock(); 113 | _connectionSelectorMock = new Mock(); 114 | 115 | var connectionToReturn = new Mock().Object; 116 | 117 | _connectionSelectorMock 118 | .Setup(m => m.Select(It.IsAny>())) 119 | .Returns(connectionToReturn); 120 | 121 | _connectionPool = new ConnectionMultiplexerPool(size, _connectionFactoryMock.Object, _connectionSelectorMock.Object); 122 | } 123 | 124 | private Task Closing_all_connections_with_allowCommandsToComplete_set_to(bool value) 125 | => _connectionPool.CloseAllAsync(value); 126 | 127 | private Task Closing_all_connections() 128 | => _connectionPool.CloseAllAsync(); 129 | 130 | private void Disposing_the_pool() 131 | => _connectionPool.Dispose(); 132 | 133 | private void Getting_the_pool_size() 134 | => _retrievedPoolSize = _connectionPool.PoolSize; 135 | 136 | private async Task Getting_a_connection() 137 | { 138 | try 139 | { 140 | _retrievedConnection = await _connectionPool.GetAsync(); 141 | } 142 | catch (Exception ex) 143 | { 144 | _exceptionThrown = ex; 145 | } 146 | } 147 | 148 | private void The_amount_of_created_connections_is(int expectedCountOfCreatedConnections) 149 | => _connectionFactoryMock.Verify(m => m.CreateAsync(It.IsAny()), Times.Exactly(expectedCountOfCreatedConnections)); 150 | 151 | private void ObjectDisposedException_was_thrown() 152 | => _exceptionThrown.Should().BeOfType(); 153 | 154 | private void The_returned_pool_size_value_is(int expectedSize) 155 | => _retrievedPoolSize.Should().Be(expectedSize); 156 | 157 | private void All_open_connections_has_been_closed_with_allowCommandsToComplete_set_to(bool expectedValue) 158 | => _createdConnectionsMocks.ForEach(m => m.Verify(mock => mock.SafeClose(expectedValue), Times.Once)); 159 | 160 | private void The_return_connection_was_chosen_by_the_selector() 161 | => _retrievedConnection.Should().BeSameAs(_connectionSelectorMock.Object.Select(null)); 162 | 163 | private Mock CreateConnectionFactoryMock() 164 | { 165 | var connectionFactoryMock = new Mock(); 166 | 167 | connectionFactoryMock 168 | .Setup(m => m.CreateAsync(It.IsAny())) 169 | .Returns(() => 170 | { 171 | var reconnectableMock = new Mock(); 172 | var connectionMock = new Mock(); 173 | reconnectableMock.Setup(m => m.Connection).Returns(connectionMock.Object); 174 | 175 | _createdConnectionsMocks.Add(connectionMock); 176 | 177 | return Task.FromResult(reconnectableMock.Object); 178 | }); 179 | 180 | return connectionFactoryMock; 181 | } 182 | 183 | private List> _createdConnectionsMocks; 184 | private ConnectionMultiplexerPool _connectionPool; 185 | private Mock _connectionSelectorMock; 186 | private Mock _connectionFactoryMock; 187 | private IReconnectableConnectionMultiplexer _retrievedConnection; 188 | private Exception _exceptionThrown; 189 | private int _retrievedPoolSize; 190 | } 191 | } 192 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/Multiplexers/InternalDispoasbleConnectionMultiplexerTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using FluentAssertions; 3 | using Moq; 4 | using NUnit.Framework; 5 | using StackExchange.Redis.MultiplexerPool.Multiplexers; 6 | using TestStack.BDDfy; 7 | 8 | namespace StackExchange.Redis.MultiplexerPool.Tests.Multiplexers 9 | { 10 | /// 11 | /// Test class for 12 | /// 13 | [TestFixture] 14 | public class InternalDispoasbleConnectionMultiplexerTests 15 | { 16 | [SetUp] 17 | public void SetUp() 18 | { 19 | _exceptionThrown = null; 20 | _connectionMultiplexerMock = new Mock(); 21 | 22 | _internalDisposableConnectionMultiplexer = new InternalDisposableConnectionMultiplexer(_connectionMultiplexerMock.Object); 23 | } 24 | 25 | [Test] 26 | public void Throws_InvalidOperationException_when_trying_to_dispose_the_connection() 27 | { 28 | this.When(_ => Disposing_the_connection()) 29 | .Then(_ => InvalidOperationException_was_thrown()) 30 | .BDDfy(); 31 | } 32 | [Test] 33 | public void Throws_InvalidOperationException_when_trying_to_close_the_connection() 34 | { 35 | this.When(_ => Closing_the_connection()) 36 | .Then(_ => InvalidOperationException_was_thrown()) 37 | .BDDfy(); 38 | } 39 | 40 | [Test] 41 | public void Throws_InvalidOperationException_when_trying_to_close_the_connection_asynchronously() 42 | { 43 | this.When(_ => Closing_the_connection_asynchronously()) 44 | .Then(_ => InvalidOperationException_was_thrown()) 45 | .BDDfy(); 46 | } 47 | 48 | [Test] 49 | public void Closes_the_connection_when_safely_closing_asynchronously() 50 | { 51 | this.When(_ => Safely_closing_the_connection_asynchronously()) 52 | .Then(_ => The_underlying_connection_was_closed_asynchronously()) 53 | .BDDfy(); 54 | } 55 | 56 | 57 | [Test] 58 | public void Closes_the_connection_when_safely_closing() 59 | { 60 | this.When(_ => Safely_closing_the_connection()) 61 | .Then(_ => The_underlying_connection_was_closed()) 62 | .BDDfy(); 63 | } 64 | 65 | private void Safely_closing_the_connection_asynchronously() 66 | => _internalDisposableConnectionMultiplexer.SafeCloseAsync(); 67 | 68 | private void Safely_closing_the_connection() 69 | => _internalDisposableConnectionMultiplexer.SafeClose(); 70 | 71 | private void Disposing_the_connection() 72 | { 73 | try 74 | { 75 | _internalDisposableConnectionMultiplexer.Dispose(); 76 | } 77 | catch (Exception ex) 78 | { 79 | _exceptionThrown = ex; 80 | } 81 | } 82 | 83 | private void Closing_the_connection() 84 | { 85 | try 86 | { 87 | _internalDisposableConnectionMultiplexer.Close(); 88 | } 89 | catch (Exception ex) 90 | { 91 | _exceptionThrown = ex; 92 | } 93 | } 94 | 95 | private void Closing_the_connection_asynchronously() 96 | { 97 | try 98 | { 99 | _internalDisposableConnectionMultiplexer.CloseAsync(); 100 | } 101 | catch (Exception ex) 102 | { 103 | _exceptionThrown = ex; 104 | } 105 | } 106 | 107 | private void The_underlying_connection_was_closed() 108 | => _connectionMultiplexerMock.Verify(m => m.Close(It.IsAny()), Times.Once); 109 | 110 | private void The_underlying_connection_was_closed_asynchronously() 111 | => _connectionMultiplexerMock.Verify(m => m.CloseAsync(It.IsAny()), Times.Once); 112 | 113 | private void InvalidOperationException_was_thrown() 114 | => _exceptionThrown.Should().BeOfType(); 115 | 116 | private Mock _connectionMultiplexerMock; 117 | private InternalDisposableConnectionMultiplexer _internalDisposableConnectionMultiplexer; 118 | private Exception _exceptionThrown; 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/Multiplexers/ReconnectableConnectionMultiplexerTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | using FluentAssertions; 5 | using Moq; 6 | using NUnit.Framework; 7 | using StackExchange.Redis.MultiplexerPool.Multiplexers; 8 | using TestStack.BDDfy; 9 | 10 | namespace StackExchange.Redis.MultiplexerPool.Tests.Multiplexers 11 | { 12 | /// 13 | /// Test class for 14 | /// 15 | [TestFixture] 16 | public class ReconnectableConnectionMultiplexerTests 17 | { 18 | [SetUp] 19 | public void SetUp() 20 | { 21 | _methodCallsOrdered = new List(); 22 | 23 | _createdConnectionMultiplexer = new Mock(); 24 | 25 | _connectionMultiplexerMock = new Mock(); 26 | _factoryMock = new Mock(); 27 | 28 | _factoryMock 29 | .Setup(m => m.CreateAsync()) 30 | .Callback(() => _methodCallsOrdered.Add(nameof(IConnectionMultiplexerFactory.CreateAsync))) 31 | .ReturnsAsync(_createdConnectionMultiplexer.Object); 32 | 33 | _connectionMultiplexerMock 34 | .Setup(m => m.SafeCloseAsync(It.IsAny())) 35 | .Callback(() => _methodCallsOrdered.Add(nameof(IInternalDisposableConnectionMultiplexer.SafeCloseAsync))) 36 | .Returns(Task.CompletedTask); 37 | 38 | _reconnectableConnectionMultiplexer = new ReconnectableConnectionMultiplexer(0, _connectionMultiplexerMock.Object, _factoryMock.Object); 39 | } 40 | 41 | [Test] 42 | [TestCase(true, true)] 43 | [TestCase(true, false)] 44 | [TestCase(false, false)] 45 | [TestCase(false, true)] 46 | public void Closes_previous_connection_only_after_a_new_one_has_been_created_when_reconnecting(bool allowCommandsToComplete, bool fireAndForgetOnClose) 47 | { 48 | this.When(_ => Reconnecting(allowCommandsToComplete, fireAndForgetOnClose)) 49 | .Then(_ => The_connection_time_has_been_updated_to_utc_now()) 50 | .And(_ => A_new_connection_has_been_created()) 51 | .And(_ => After_that_the_previous_connection_has_been_closed()) 52 | .BDDfy(); 53 | } 54 | 55 | private void The_connection_time_has_been_updated_to_utc_now() 56 | => _reconnectableConnectionMultiplexer.ConnectionTimeUtc.Should().BeCloseTo(DateTime.UtcNow, TimeSpan.FromMilliseconds(100)); 57 | 58 | private Task Reconnecting(bool allowCommandsToComplete, bool fireAndForgetOnClose) 59 | => _reconnectableConnectionMultiplexer.ReconnectAsync(allowCommandsToComplete, fireAndForgetOnClose); 60 | 61 | private void A_new_connection_has_been_created() 62 | => _factoryMock.Verify(m => m.CreateAsync(), Times.Once); 63 | 64 | private void After_that_the_previous_connection_has_been_closed() 65 | { 66 | _methodCallsOrdered.Should().HaveCount(2); 67 | _methodCallsOrdered[0].Should().Be(nameof(IConnectionMultiplexerFactory.CreateAsync)); 68 | _methodCallsOrdered[1].Should().Be(nameof(IInternalDisposableConnectionMultiplexer.SafeCloseAsync)); 69 | } 70 | 71 | 72 | 73 | private Mock _connectionMultiplexerMock; 74 | private ReconnectableConnectionMultiplexer _reconnectableConnectionMultiplexer; 75 | private Mock _createdConnectionMultiplexer; 76 | private Mock _factoryMock; 77 | 78 | private List _methodCallsOrdered; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /tests/StackExchange.Redis.MultiplexerPool.Tests/StackExchange.Redis.MultiplexerPool.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6 5 | StackExchange.Redis.MultiplexerPool.Tests 6 | StackExchange.Redis.MultiplexerPool.Tests 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 | --------------------------------------------------------------------------------