csredis
csredis copied to clipboard
偶现 CSRedis.RedisClientException: Connection was not opened
CSRedis.RedisClientException: Connection was not opened
at CSRedis.CSRedisClient.GetAndExecuteAsync[T](RedisClientPool pool, Func`2 handerAsync, Int32 jump, Int32 errtimes)
at CSRedis.CSRedisClient.ExecuteScalarAsync[T](String key, Func`3 handerAsync)
报上述错误的同时也会报如下错误
System.Exception: 【redis:6379/0】状态不可用,等待后台检查程序恢复方可使用。Connect to server timeout
---> CSRedis.Internal.IO.RedisSocketException: Connect to server timeout
at CSRedis.Internal.IO.RedisSocket.Connect(EndPoint endpoint, Int32 timeout)
at CSRedis.Internal.RedisConnector.Connect(Int32 timeout)
at CSRedis.RedisClientPool.Return(Object`1 obj, Exception exception, Boolean isRecreate)
--- End of inner exception stack trace ---
at CSRedis.Internal.ObjectPool.ObjectPool`1.getFree(Boolean checkAvailable)
at CSRedis.Internal.ObjectPool.ObjectPool`1.GetAsync()
at CSRedis.CSRedisClient.GetClientAsync(RedisClientPool pool)
at CSRedis.CSRedisClient.GetAndExecuteAsync[T](RedisClientPool pool, Func`2 handerAsync, Int32 jump, Int32 errtimes)
at CSRedis.CSRedisClient.ExecuteScalarAsync[T](String key, Func`3 handerAsync)
应用程序和redis部署在一台服务器上的两个docker里,这个是偶发的错误,现在访问量不大,redis连接字符串如下 redis:6379,password=xxxxxxxx,defaultdatabase=0,allowAdmin=true,autodispose=false,poolsize=50,preheat=3,tryit=2

这个错误是连接超时
用的是域名,还是ip
是docker-compose部署的,域名redis是有个名称为redis的容器。大致是这样的
version: '3.8' services:
redis: image: redis:7.0.0 container_name: redis
eyeapp: image:
csredis版本是3.6.9 redis版本7.0
我也遇到了一样的错误,用的是阿里云Redis 2G主从版.
csredis版本: 3.6.9
redis版本: 5.0
连接字符串: 123abc.redis.rds.aliyuncs.com:6379,password=123456,defaultDatabase=0,autoDispose=false
我的是程序和redis部署在同一台阿里云 ecs的两个docker里的,本地测试环境相同的部署方式没有出现过这个问题,怀疑是阿里云服务器操作系统优化过导致docker自带dns出问题导致的,我把连接字符串改成本机ip,然后redis的docker通过host模式部署到现在为止还没出现过这个问题
升级到.net6,csredis更新升级到3.8.2还是存在同样的问题,几乎每天都会遇到
状态不可用,等待后台检查程序恢复方可使用.还会出现这个错误
Exception(CSRedisCore: 6/29/2022 5:33:57 PM) -> RedisClientException: Connection was not opened
at CSRedis.CSRedisClient.GetAndExecuteAsync[T](RedisClientPool pool, Func`2 handerAsync, Int32 jump, Int32 errtimes)
at CSRedis.CSRedisClient.ExecuteScalarAsync[T](String key, Func`3 handerAsync)
at Server.Services.RedisCacheService.Server.Services.IRedisCacheService.Exists(String key) in /Users/jenkins/.jenkins/workspace/UniStory-Server-OutsideRelease-New/Server/Server/Services/RedisCacheService.cs:line 78
at Server.Services.CommentCacheService.GetTopic(String topic) in /Users/jenkins/.jenkins/workspace/UniStory-Server-OutsideRelease-New/Server/Server/Services/CommentCacheService.cs:line 179
at Server.Services.CommentService.GetTopic(String topic) in /Users/jenkins/.jenkins/workspace/UniStory-Server-OutsideRelease-New/Server/Server/Services/CommentService.cs:line 31
at Server.Controllers.CommentController.GetTopicCommentSummary(RequestModel requestModel) in /Users/jenkins/.jenkins/workspace/UniStory-Server-OutsideRelease-New/Server/Server/Controllers/CommentController.cs:line 45
at lambda_method(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextExceptionFilterAsync>g__Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
我们也遇到了同样的问题 ASP.NET Core 3.1 CSRedisCore v3.6.9
用的连接参数 abortConnect=false,poolsize=100,preheat=8,tryit=2 使用AWS的ElasticCache服务
这个问题有任何进展吗?
连接串:testcluster=false
看你们描述都是云,他的内部可能做成了集群,导致连接了内部ip
连接串加上了:testcluster=false 还是一样,测试环境知己装的redis服务,不是用的云服务,也会偶发出现
【127.0.0.1:6230/2】状态不可用,等待后台检查程序恢复方可使用。Connect to server timeout
2023/7/5 11:35:47
【127.0.0.1:6230/2】状态不可用,等待后台检查程序恢复方可使用。Connect to server timeout
Type:System.Exception
StackTrace:
在 CSRedis.Internal.ObjectPool.ObjectPool1.GetFree(Boolean checkAvailable) 在 CSRedis.Internal.ObjectPool.ObjectPool1.Get(Nullable1 timeout) 在 CSRedis.CSRedisClient.GetAndExecute[T](RedisClientPool pool, Func2 handler, Int32 jump, Int32 errtimes)
在 CSRedis.CSRedisClient.ExecuteScalar[T](String key, Func`3 hander)
在 CSRedis.CSRedisClient.Get(String key)
.net 6 最新版本3.8.670
@2881099
查看源代码 Connection was not opened出错是 RedisIO里的三个对象的异常导致 public RedisReader Reader => GetOrThrow(_reader); public Stream Stream => GetOrThrow(_stream); public RedisPipeline Pipeline => GetOrThrow(_pipeline);
static T GetOrThrow(T obj) { if (obj == null) throw new RedisClientException("Connection was not opened"); return obj; }
这RedisIO对象是在 RedisClientPool对象中建立的。应该和池有关,高并发的情况下不知啥原因导致RedisReader ,Stream ,RedisPipeline 的对象丢失导致Connection was not opened错误
处理方式: 1、调整池的大小,满足实际需要。比连接数量要大,再加上设置redis配置文件 timeout,回收闲置连接 2、在出现Connection was not opened异常时,重现建立一个 CSRedis.CSRedisClient对象,建立 CSRedis.CSRedisClient的过程中会也重新RedisClientPool,以及RedisReader ,Stream ,RedisPipeline三个对象,从而使程序正常。 如下: csRedis_DB 是一个单例全局变量 连接函数如下: if (csRedis_DB == null ) { lock (csRedis_Lock) { if (csRedis_DB == null)// { csRedis_DB = new CSRedis.CSRedisClient(RedisDB_Conn); } } }
调用函数如下: try { Connect_csRedis(); rv = csRedis_DB.HGet("xxxxRealTime", cardid.ToString()); if (rv != null && rv != "") { data = JsonConvert.DeserializeObject(rv); } } catch (Exception err) { csRedis_DB = null }
具体效果还在测试。大家可以尝试验证一下
建议切换到 FreeRedis
最后的进展就是切换到了StackExchange.Redis,1年多了也没啥问题,redis的部署方式啥都没变