Enable reconnect for Redis connection by default (#1037)

This commit is contained in:
BrennanConroy 2017-10-24 14:30:13 -07:00 committed by GitHub
parent 1768a081ba
commit 30e7422407
3 changed files with 67 additions and 6 deletions

View File

@ -2,7 +2,9 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Linq;
using Microsoft.Extensions.Logging;
using StackExchange.Redis;
namespace Microsoft.AspNetCore.SignalR.Redis.Internal
{
@ -27,9 +29,24 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Internal
private static readonly Action<ILogger, string, Exception> _unsubscribe =
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, nameof(Unsubscribe)), "Unsubscribing from channel: {channel}.");
public static void ConnectingToEndpoints(this ILogger logger, string endpoints)
private static readonly Action<ILogger, Exception> _notConnected =
LoggerMessage.Define(LogLevel.Warning, new EventId(6, nameof(Connected)), "Not connected to Redis.");
private static readonly Action<ILogger, Exception> _connectionRestored =
LoggerMessage.Define(LogLevel.Information, new EventId(7, nameof(ConnectionRestored)), "Connection to Redis restored.");
private static readonly Action<ILogger, Exception> _connectionFailed =
LoggerMessage.Define(LogLevel.Warning, new EventId(8, nameof(ConnectionFailed)), "Connection to Redis failed.");
public static void ConnectingToEndpoints(this ILogger logger, EndPointCollection endpoints)
{
_connectingToEndpoints(logger, endpoints, null);
if (logger.IsEnabled(LogLevel.Information))
{
if (endpoints.Count > 0)
{
_connectingToEndpoints(logger, string.Join(", ", endpoints.Select(e => EndPointCollection.ToString(e))), null);
}
}
}
public static void Connected(this ILogger logger)
@ -56,5 +73,20 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Internal
{
_unsubscribe(logger, channelName, null);
}
public static void NotConnected(this ILogger logger)
{
_notConnected(logger, null);
}
public static void ConnectionRestored(this ILogger logger)
{
_connectionRestored(logger, null);
}
public static void ConnectionFailed(this ILogger logger, Exception exception)
{
_connectionFailed(logger, exception);
}
}
}

View File

@ -51,16 +51,40 @@ namespace Microsoft.AspNetCore.SignalR.Redis
_ackHandler = new AckHandler();
var writer = new LoggerTextWriter(logger);
_logger.ConnectingToEndpoints(string.Join(", ", options.Value.Options.EndPoints.Select(e => EndPointCollection.ToString(e))));
_logger.ConnectingToEndpoints(options.Value.Options.EndPoints);
_redisServerConnection = _options.Connect(writer);
_redisServerConnection.ConnectionRestored += (_, e) =>
{
// We use the subscription connection type
// Ignore messages from the interactive connection (avoids duplicates)
if (e.ConnectionType == ConnectionType.Interactive)
{
return;
}
_logger.ConnectionRestored();
};
_redisServerConnection.ConnectionFailed += (_, e) =>
{
// We use the subscription connection type
// Ignore messages from the interactive connection (avoids duplicates)
if (e.ConnectionType == ConnectionType.Interactive)
{
return;
}
_logger.ConnectionFailed(e.Exception);
};
if (_redisServerConnection.IsConnected)
{
_logger.Connected();
}
else
{
// TODO: We could support reconnecting, like old SignalR does.
throw new InvalidOperationException("Connection to redis failed.");
_logger.NotConnected();
}
_bus = _redisServerConnection.GetSubscriber();

View File

@ -10,7 +10,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis
{
public class RedisOptions
{
public ConfigurationOptions Options { get; set; } = new ConfigurationOptions();
public ConfigurationOptions Options { get; set; } = new ConfigurationOptions
{
// Enable reconnecting by default
AbortOnConnectFail = false
};
public Func<TextWriter, IConnectionMultiplexer> Factory { get; set; }
@ -25,6 +29,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
Options.EndPoints.Add(IPAddress.Loopback, 0);
Options.SetDefaultPorts();
}
return ConnectionMultiplexer.Connect(Options, log);
}