public RedisHubLifetimeManager(ILogger <RedisHubLifetimeManager <THub> > logger, IOptions <RedisOptions> options, IHubProtocolResolver hubProtocolResolver) { _logger = logger; _options = options.Value; _ackHandler = new AckHandler(); _channels = new RedisChannels(typeof(THub).FullName); _protocol = new RedisProtocol(hubProtocolResolver.AllProtocols); RedisLog.ConnectingToEndpoints(_logger, options.Value.Configuration.EndPoints, _serverName); _ = EnsureRedisServerConnection(); }
private async Task EnsureRedisServerConnection() { if (_defaultServer == null || _shardingServers == null) { await _connectionLock.WaitAsync(); try { if (_defaultServer == null || _shardingServers == null) { var writer = new LoggerTextWriter(_logger); var tasks = _options.Configurations.Select(async configuration => { var serverName = _options.ServerNameGenerator.GenerateServerName(); RedisLog.ConnectingToEndpoints(_logger, configuration.Options.EndPoints, serverName); var redisConnection = await _options.ConnectAsync(configuration.Options, writer); var server = new ShardingRedisServer(serverName, configuration.IsDedicatedForAllChannel, redisConnection, _logger); SubscribeToGroupManagementChannel(server); SubscribeToAckChannel(server); return(server); }).ToArray(); var redisServers = await Task.WhenAll(tasks); _defaultServer = redisServers.FirstOrDefault(server => server.IsDedicatedForAllChannel) ?? redisServers.First(); SubscribeToAll(_defaultServer); var shardingServers = redisServers.Where(server => !server.IsDedicatedForAllChannel); _shardingServers = shardingServers.ToArray(); } } finally { _connectionLock.Release(); } } }
/// <summary> /// Constructs the <see cref="RedisHubLifetimeManager{THub}"/> with types from Dependency Injection. /// </summary> /// <param name="logger">The logger to write information about what the class is doing.</param> /// <param name="options">The <see cref="RedisOptions"/> that influence behavior of the Redis connection.</param> /// <param name="hubProtocolResolver">The <see cref="IHubProtocolResolver"/> to get an <see cref="IHubProtocol"/> instance when writing to connections.</param> /// <param name="globalHubOptions">The global <see cref="HubOptions"/>.</param> /// <param name="hubOptions">The <typeparamref name="THub"/> specific options.</param> public RedisHubLifetimeManager(ILogger <RedisHubLifetimeManager <THub> > logger, IOptions <RedisOptions> options, IHubProtocolResolver hubProtocolResolver, IOptions <HubOptions>?globalHubOptions, IOptions <HubOptions <THub> >?hubOptions) { _logger = logger; _options = options.Value; _ackHandler = new AckHandler(); _channels = new RedisChannels(typeof(THub).FullName !); if (globalHubOptions != null && hubOptions != null) { _protocol = new RedisProtocol(new DefaultHubMessageSerializer(hubProtocolResolver, globalHubOptions.Value.SupportedProtocols, hubOptions.Value.SupportedProtocols)); } else { var supportedProtocols = hubProtocolResolver.AllProtocols.Select(p => p.Name).ToList(); _protocol = new RedisProtocol(new DefaultHubMessageSerializer(hubProtocolResolver, supportedProtocols, null)); } RedisLog.ConnectingToEndpoints(_logger, options.Value.Configuration.EndPoints, _serverName); _ = EnsureRedisServerConnection(); }