samcook / RedLock.net

An implementation of the Redlock algorithm in C#
MIT License
947 stars 167 forks source link

API doesn't start when trying to init StackExchange.Redis ConnectionMultiplexer (using RedLock too) #73

Closed weare8 closed 4 years ago

weare8 commented 4 years ago

Having built a 3 node Redis cluster with Sentinel in front and using StackExchange.Redis with the below configuration.

"Redis": {
    "Password": "super_secret_password",
    "AllowAdmin": true,
    "Ssl": false,
    "ConnectTimeout": 3000,
    "ConnectRetry": 2,
    "Database": 0,
    "Hosts": [
      {
        "Host": "redis-1.domain.com",
        "Port": 26379
      },
      {
        "Host": "redis-2.domain.com",
        "Port": 26379
      },
      {
        "Host": "redis-3.domain.com",
        "Port": 26379
      }
    ]
  }

Configuring the DI container.

var redisConfiguration = configuration.GetSection(redisSettingsJsonSection).Get<RedisConfiguration>();
services.AddSingleton(redisConfiguration);
services.AddSingleton<IRedisCacheClient, RedisCacheClient>();
services.AddSingleton<IRedisCacheConnectionPoolManager, RedisCacheConnectionPoolManager>();
services.AddSingleton(_ => new Utf8JsonSerializer());
services.AddSingleton(provider =>
                provider.GetService<IRedisCacheClient>().GetDbFromConfiguration());

// Setting up RedLock for distributed locking support, not sure if the below is correct

services.AddSingleton<IDistributedLockFactory>(provider =>
                RedLockFactory.Create(new List<RedLockMultiplexer>()
                {
                    new RedLockMultiplexer(
                        (ConnectionMultiplexer) provider.GetRequiredService<IRedisCacheConnectionPoolManager>()
                            .GetConnection())
                })
            );

When I start the ASP.NET Core API 3.1 I get the below messages and it just hangs.

dbug: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] Checking if there are any invalid connections... dbug: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] The pool size is 5 and it requires new 5 connections. dbug: StackExchange.Redis.Extensions.Core.Implementations.RedisCacheConnectionPoolManager[0] Creating new Redis connection.

All nugget packages are the latest.

weare8 commented 4 years ago

Actually I believe I figured this out with the below. This is not a RedLock issue, more of setting up Sentinel configuration options and the ConnectionMultiplexer and passing the below for RedLock (pending more testing).

services.AddSingleton<IDistributedLockFactory>(provider =>
                RedLockFactory.Create(new List<RedLockMultiplexer>
                {
                    new RedLockMultiplexer(provider.GetRequiredService<ConnectionMultiplexer>())
                })
            );