Enable reconnect for Redis connection by default (#1037)
This commit is contained in:
parent
1768a081ba
commit
30e7422407
|
|
@ -2,7 +2,9 @@
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
|
||||||
namespace Microsoft.AspNetCore.SignalR.Redis.Internal
|
namespace Microsoft.AspNetCore.SignalR.Redis.Internal
|
||||||
{
|
{
|
||||||
|
|
@ -27,9 +29,24 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Internal
|
||||||
private static readonly Action<ILogger, string, Exception> _unsubscribe =
|
private static readonly Action<ILogger, string, Exception> _unsubscribe =
|
||||||
LoggerMessage.Define<string>(LogLevel.Trace, new EventId(5, nameof(Unsubscribe)), "Unsubscribing from channel: {channel}.");
|
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)
|
public static void Connected(this ILogger logger)
|
||||||
|
|
@ -56,5 +73,20 @@ namespace Microsoft.AspNetCore.SignalR.Redis.Internal
|
||||||
{
|
{
|
||||||
_unsubscribe(logger, channelName, null);
|
_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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -51,16 +51,40 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
_ackHandler = new AckHandler();
|
_ackHandler = new AckHandler();
|
||||||
|
|
||||||
var writer = new LoggerTextWriter(logger);
|
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 = _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)
|
if (_redisServerConnection.IsConnected)
|
||||||
{
|
{
|
||||||
_logger.Connected();
|
_logger.Connected();
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// TODO: We could support reconnecting, like old SignalR does.
|
_logger.NotConnected();
|
||||||
throw new InvalidOperationException("Connection to redis failed.");
|
|
||||||
}
|
}
|
||||||
_bus = _redisServerConnection.GetSubscriber();
|
_bus = _redisServerConnection.GetSubscriber();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,11 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
{
|
{
|
||||||
public class RedisOptions
|
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; }
|
public Func<TextWriter, IConnectionMultiplexer> Factory { get; set; }
|
||||||
|
|
||||||
|
|
@ -25,6 +29,7 @@ namespace Microsoft.AspNetCore.SignalR.Redis
|
||||||
Options.EndPoints.Add(IPAddress.Loopback, 0);
|
Options.EndPoints.Add(IPAddress.Loopback, 0);
|
||||||
Options.SetDefaultPorts();
|
Options.SetDefaultPorts();
|
||||||
}
|
}
|
||||||
|
|
||||||
return ConnectionMultiplexer.Connect(Options, log);
|
return ConnectionMultiplexer.Connect(Options, log);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue