Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ private static async Task<ConnectionMultiplexer> SentinelPrimaryConnectAsync(Con
/// <param name="log">The writer to log to, if any.</param>
public ConnectionMultiplexer GetSentinelMasterConnection(ConfigurationOptions config, TextWriter? log = null)
{
if (ServerSelectionStrategy.ServerType != ServerType.Sentinel)
if (ServerSelectionStrategy.ServerType != ServerType.Sentinel
&& (RawConfig.AbortOnConnectFail || IsConnected))
{
throw new RedisConnectionException(
ConnectionFailureType.UnableToConnect,
Expand Down Expand Up @@ -203,6 +204,18 @@ public ConnectionMultiplexer GetSentinelMasterConnection(ConfigurationOptions co

if (newPrimaryEndPoint is null)
{
if (!config.AbortOnConnectFail)
{
connection = ConnectImpl(config, log, endpoints: config.EndPoints.Clone());
connection.ConnectionRestored += OnManagedConnectionRestored;
connection.ConnectionFailed += OnManagedConnectionFailed;
lock (sentinelConnectionChildren)
{
sentinelConnectionChildren[serviceName] = connection;
}
return connection;
}

throw new RedisConnectionException(
ConnectionFailureType.UnableToConnect,
$"Sentinel: Failed connecting to configured primary for service: {config.ServiceName}");
Expand Down
41 changes: 41 additions & 0 deletions tests/StackExchange.Redis.Tests/SentinelAbortOnConnectTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Threading.Tasks;
using Xunit;

namespace StackExchange.Redis.Tests;

public class SentinelAbortOnConnectTests(ITestOutputHelper output) : TestBase(output)
{
[Fact]
public void ConnectToUnreachableSentinelReturnsDisconnectedMultiplexer()
{
var config = ConfigurationOptions.Parse("nonexistent:26379,serviceName=mymaster");
config.AbortOnConnectFail = false;
config.ConnectTimeout = 1000;

using var mux = ConnectionMultiplexer.Connect(config);
Assert.NotNull(mux);
Assert.False(mux.IsConnected);
}

[Fact]
public async Task ConnectAsyncToUnreachableSentinelReturnsDisconnectedMultiplexer()
{
var config = ConfigurationOptions.Parse("nonexistent:26379,serviceName=mymaster");
config.AbortOnConnectFail = false;
config.ConnectTimeout = 1000;

await using var mux = await ConnectionMultiplexer.ConnectAsync(config);
Assert.NotNull(mux);
Assert.False(mux.IsConnected);
}

[Fact]
public void ConnectToUnreachableSentinelThrowsWhenAbortOnConnectFailIsTrue()
{
var config = ConfigurationOptions.Parse("nonexistent:26379,serviceName=mymaster");
config.AbortOnConnectFail = true;
config.ConnectTimeout = 1000;

Assert.Throws<RedisConnectionException>(() => ConnectionMultiplexer.Connect(config));
}
}
Loading