public void RedisConnectionExceptionIsKnownUnobservedException() { var redisException = new RedisConnectionException("Message"); Assert.True( ExceptionUtilities.IsKnownUnobservedException(redisException)); Assert.True(ExceptionUtilities.IsKnownUnobservedException(new AggregateException(new Exception("1"), redisException))); }
private static RetryPolicy ReConnectPolicy(string connectionString, ILogger logger) { //TODO: move this settings to options return(Policy .Handle <RedisConnectionException>() .WaitAndRetryForever( count => TimeSpan.FromSeconds(5), (exception, retryCount, timeSpan) => { RedisConnectionException ex = (RedisConnectionException)exception; logger.LogCritical(exception, $"Redis 建立链接时失败 Connection lost. Try {retryCount}th times. Wait For {timeSpan.TotalSeconds} seconds. Redis Can not connect {connectionString}"); })); }
/// <summary> /// Processing method of RedisConnectionException. /// </summary> /// <param name="ex">Instance RedisConnectionException.</param> public void ProcessingException(RedisConnectionException ex) { var swRedisExceptionLog = new StreamWriter("RedisExceptionLog.txt", true); swRedisExceptionLog.WriteLine("#########################################################################################"); swRedisExceptionLog.WriteLine($"RedisException DateTime: {DateTime.Now}\n"); swRedisExceptionLog.WriteLine($"RedisException Data: {ex.Data}\n"); swRedisExceptionLog.WriteLine($"RedisException HelpLink: {ex.HelpLink}\n"); swRedisExceptionLog.WriteLine($"RedisException HResult: {ex.HResult}\n"); swRedisExceptionLog.WriteLine($"RedisException InnerException: {ex.InnerException}\n"); swRedisExceptionLog.WriteLine($"RedisException Message: {ex.Message}\n"); swRedisExceptionLog.WriteLine($"RedisException Source: {ex.Source}\n"); swRedisExceptionLog.WriteLine($"RedisException StackTrace: {ex.StackTrace}\n"); swRedisExceptionLog.WriteLine($"RedisException TargetSite: {ex.TargetSite}\n"); swRedisExceptionLog.Close(); }
private void StartNoCacheMode(RedisConnectionException ex) { if (!_existsConnectionError) { _existsConnectionError = true; this.CreateBackCache(); this._logger.WriteError($"无法连接 Redis 服务器 {_options.ConnectionString} ,进入本机缓存模式,{_options.ReconnectFrequencySeconds} 秒后断线重连开始执行。", ex); lock (_retryLock) { _timer?.Dispose(); _timer = null; } _timer = new Timer(this.RetryConnection, null, TimeSpan.FromSeconds(_options.ReconnectFrequencySeconds), TimeSpan.FromSeconds(_options.ReconnectFrequencySeconds)); } }
/// <summary> /// AddDataProtectionWithCertInRedis /// </summary> /// <param name="services"></param> /// <param name="action"></param> /// <returns></returns> /// <exception cref="WebApiException"></exception> public static IServiceCollection AddDataProtectionWithCertInRedis(this IServiceCollection services, Action <DataProtectionSettings> action) { DataProtectionSettings dataProtectionSettings = new DataProtectionSettings(); action(dataProtectionSettings); string redisKey = $"{dataProtectionSettings.ApplicationName}_{EnvironmentUtil.AspNetCoreEnvironment}_dpk"; X509Certificate2 certificate2 = CertificateUtil.GetCertificateFromSubjectOrFile( dataProtectionSettings.CertificateSubject, dataProtectionSettings.CertificateFileName, dataProtectionSettings.CertificateFilePassword); ConfigurationOptions redisConfigurationOptions = ConfigurationOptions.Parse(dataProtectionSettings.RedisConnectString); redisConfigurationOptions.AllowAdmin = false; Policy .Handle <RedisConnectionException>() .WaitAndRetryForever( count => TimeSpan.FromSeconds(5 + count * 2), (exception, retryCount, timeSpan) => { RedisConnectionException ex = (RedisConnectionException)exception; Log.Fatal( exception, $"DataProtection : Try {retryCount}th times. Wait For {timeSpan.TotalSeconds} seconds. Redis Can not connect {dataProtectionSettings.RedisConnectString} : {redisKey};" ); }) .Execute(() => { ConnectionMultiplexer redisMultiplexer = ConnectionMultiplexer.Connect(redisConfigurationOptions); services .AddDataProtection() .SetApplicationName(dataProtectionSettings.ApplicationName) .ProtectKeysWithCertificate(certificate2) .PersistKeysToStackExchangeRedis(redisMultiplexer, redisKey); }); return(services); }
private bool IsSocketOrRedisTimeOutException(Exception callError) { RedisConnectionException connectionException = callError as RedisConnectionException; if (null != connectionException) { //should retry if the db is still loading if (connectionException.FailureType == ConnectionFailureType.Loading) { return(true); } } //always retry on a timeout, this is sometimes a problem //with initial connections but allows for cluster reconfiguration //when a multiplexer is repointed to a different server RedisTimeoutException redisException = callError as RedisTimeoutException; if (null != redisException) { return(true); } SocketException socketException = callError as SocketException; if (null != socketException) { //retry on these if (socketException.SocketErrorCode == SocketError.TimedOut || socketException.SocketErrorCode == SocketError.InProgress || socketException.SocketErrorCode == SocketError.IOPending) { return(true); } } return(false); }
internal static Exception ConnectionFailure(bool includeDetail, ConnectionFailureType failureType, string message, ServerEndPoint server) { var ex = new RedisConnectionException(failureType, message); if (includeDetail) AddDetail(ex, null, server, null); return ex; }
internal static Exception NoConnectionAvailable(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server, ServerEndPoint[] serverSnapshot) { string s = GetLabel(includeDetail, command, message); if (server != null) { //if we already have the serverEndpoint for connection failure use that //otherwise it would output state of all the endpoints serverSnapshot = new ServerEndPoint[] { server }; } string exceptionmessage = "No connection is available to service this operation: " + s ; var ex = new RedisConnectionException(ConnectionFailureType.UnableToResolvePhysicalConnection, exceptionmessage, GetServerSnapshotInnerExceptions(serverSnapshot)); if (includeDetail) { AddDetail(ex, message, server, s); } return ex; }
internal static Exception NoConnectionAvailable(bool includeDetail, RedisCommand command, Message message, ServerEndPoint server) { string s = GetLabel(includeDetail, command, message); var ex = new RedisConnectionException(ConnectionFailureType.UnableToResolvePhysicalConnection, "No connection is available to service this operation: " + s); if (includeDetail) AddDetail(ex, message, server, s); return ex; }