Skip to content

Setting AbortConnectOnFail doesn't work for Redis Sentinel setups #2173

@pietervdheijden

Description

@pietervdheijden

Issue
Setting AbortConnectOnFail doesn't work for Redis Sentinel setups.

Details
Setting AbortConnectOnFail=false works perfectly in Redis Master Slave setups. For example, this code:

// Startup.cs
var redisOptions = ConfigurationOptions.Parse("127.0.0.1:6379");
redisOptions.Password = "dev";
redisOptions.AbortOnConnectFail = false;
var redisConnection = ConnectionMultiplexer.Connect(redisOptions);

does not throw an exception when Redis is unavailable.

However, the setting does not work in Redis Sentinel setups. For example, this code:

// Startup.cs
var redisOptions = ConfigurationOptions.Parse("127.0.0.1:26379,serviceName=mymaster");
redisOptions.Password = "dev";
redisOptions.AbortOnConnectFail = false;
var redisConnection = ConnectionMultiplexer.Connect(redisOptions);

throws the following exception when Redis Sentinel is unavailable:

Unhandled exception. StackExchange.Redis.RedisConnectionException: Sentinel: The ConnectionMultiplexer is not a Sentinel connection. Detected as: Standalone
at StackExchange.Redis.ConnectionMultiplexer.GetSentinelMasterConnection(ConfigurationOptions config, TextWriter log) in //src/StackExchange.Redis/ConnectionMultiplexer.Sentinel.cs:line 163
at StackExchange.Redis.ConnectionMultiplexer.Connect(ConfigurationOptions configuration, TextWriter log) in /
/src/StackExchange.Redis/ConnectionMultiplexer.cs:line 675

Context
I'm trying to set up the Redis configuration in Startup.cs to register Singleton IConnectionMultiplexer and to configure .NET's data protection library to persist keys in Redis:

services.AddSingleton<IConnectionMultiplexer>(_ => ConnectionMultiplexer.Connect(redisOptions));
services.AddDataProtection()
    .PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(redisOptions), "DataProtection-Keys");

When method ConnectionMultiplexer.Connect(redisOptions) throws an exception in the Startup.cs, the .NET application will abort. As a result, it's not possible to properly implement liveness and readiness (i.e. live = app is running, ready = app is running and all dependencies are available).

Analysis
I've tried to resolve this error myself, but unfortunately I didn't succeed... I think that the fix should be to move method GetSentinelMasterConnection() (see: ConnectionMultiplexer.Sentinel.cs#L133-L135) into method ReconfigureAsync() (see: ConnectionMultiplexer.cs#L1178-L1506). So, if the master node and/or the sentinel node are not available during startup, the connections will be regenerated during reconfiguration. Please let me know if I can assist in solving the issue!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions