我在应用程序中实现了stackexchange.redis,从redis db list中获取数据。登录应用程序(wpf)创建一个列表,并将另一个模块(c++)获取的数据推送到创建redisdb的服务器上。服务器模块将响应推送到特定于用户的列表,wpf模块从中每隔10毫秒通过轮询读取数据。
我创建了以下方法来执行这些任务。
建造师
public RedisConnection()
{
try
{
ConnectionMultiplexer.SetFeatureFlag("preventthreadtheft", true);
if (_databaseRedis != null)
this.DatabaseRedis = _databaseRedis;
if (_clientSubscriptionChannnel != null)
this.ClientSubscriptionChannnel = _clientSubscriptionChannnel;
}
catch (Exception ex)
{
}
}
连接redis
public bool CreateConnection(Hashtable ParamsHt)
{
try
{
var settings = ConfigurationOptions.Parse(ParamsHt["RedisIP"].ToString());
settings.ConnectRetry = 3;
settings.ConnectTimeout = 10000;
settings.SyncTimeout = 20000;
connection = ConnectionMultiplexer.Connect(settings);
if (connection.IsConnected)
{
DatabaseRedis = connection.GetDatabase();
}
return isConnected = connection.IsConnected;
}
catch (Exception ex)
{
return false;
}
}
从列表中获取数据
private void FetchListToProcess()
{
try
{
IDatabase database = connection.GetDatabase();
long length = database.ListLength(ConfigurationService.ListToInsertRequest + "_" + LoginID);
if (length > 0)
{
RedisValue rv = database.ListLeftPop(ConfigurationService.ListToInsertRequest+"_"+ LoginID, CommandFlags.None);
if (rv.HasValue)
{
database.ListRemove(LoginID, rv);
byte[] userObject = (byte[])rv;
CommonOM.MessageHeader HeaderObj = CommonBL.ByteArrayToStructure<CommonOM.MessageHeader>(userObject);
int MessageType = HeaderObj.messageCode;
if (MessageType == Convert.ToInt32(TRANSACTION_REQUEST_CODE.HEART_BEAT))
{
delRecieveReadyRedisHearbeat delHeartbeatReciever = new delRecieveReadyRedisHearbeat(_heartbeatHelper.OnRecieveHeartbeat);
delHeartbeatReciever.BeginInvoke(MessageType, userObject, null, null);
}
else
{
delRecieveReadyRedisHearbeat delHeartbeatReciever = new delRecieveReadyRedisHearbeat(_heartbeatHelper.OnRecieveHeartbeat);
delHeartbeatReciever.BeginInvoke(MessageType, userObject, null, null);
}
else
{
delRecieveReadyRedisMessages delReady = new delRecieveReadyRedisMessages(_connectionHelper.OnReceive);
delReady.BeginInvoke(MessageType, userObject, null, null);
}
}
}
}
catch (StackExchange.Redis.RedisTimeoutException ex)
{
}
catch (Exception ex)
{
}
}
将数据插入列表
public void InsertIntoList(byte[] messageToSend,bool isLoginRequest,string loginID)
{
try
{
IDatabase database = connection.GetDatabase();
database.ListRightPush(ConfigurationService.ListToInsertRequest, messageToSend);
}
catch (RedisTimeoutException ex)
{
}
catch (Exception ex)
{
}
}
在创建连接之后,我每隔10毫秒启动一个system.timer调用fetchlisttoprocess()方法,该方法检查列表中是否存在任何数据,如果存在,它将相应地进行提取和处理。
我遇到应用程序随机记录超时异常的问题:
Error Messg :: Timeout performing LLEN (5000ms), next: LLEN ROUTER_IEORALG9, inst: 2, qu: 0, qs: 40, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=1,Free=999,Min=8,Max=1000), WORKER: (Busy=20,Free=32747,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:26.93 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListLength(RedisKey key, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 943
at BL.RedisConnection.FetchListToProcess()
03:29:27.43 :: Method Name :: Redis --> RedisTimeoutException
03:29:27.43 :: Error Messg :: Timeout performing LLEN (5000ms), next: LLEN ROUTER_IEORALG9, inst: 3, qu: 0, qs: 41, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=21,Free=32746,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:27.43 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListLength(RedisKey key, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 943
at BL.RedisConnection.FetchListToProcess()
03:29:28.94 :: Method Name :: Redis --> RedisTimeoutException
03:29:28.94 :: Error Messg :: Timeout performing LLEN (5000ms), next: LLEN ROUTER_IEORALG9, inst: 1, qu: 0, qs: 44, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=22,Free=32745,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:28.94 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListLength(RedisKey key, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 943
at BL.RedisConnection.FetchListToProcess()
03:29:29.36 :: Method Name :: Redis --> RedisTimeoutException
03:29:29.36 :: Error Messg :: Timeout performing LLEN (5000ms), next: LLEN ROUTER_IEORALG9, inst: 2, qu: 0, qs: 45, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=0,Free=1000,Min=8,Max=1000), WORKER: (Busy=22,Free=32745,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:29.36 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListLength(RedisKey key, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 943
at BL.RedisConnection.FetchListToProcess()
03:29:29.42 :: Method Name :: Redis --> RedisTimeoutException
03:29:29.42 :: Error Messg :: Timeout performing LLEN (5000ms), next: LLEN ROUTER_IEORALG9, inst: 3, qu: 0, qs: 46, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=1,Free=999,Min=8,Max=1000), WORKER: (Busy=22,Free=32745,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:29.42 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListLength(RedisKey key, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 943
at BL.RedisConnection.FetchListToProcess()
以及insert-into-list方法
03:29:26.42 :: Method Name :: RedisConnection --> InsertIntoList
03:29:26.42 :: Error Messg :: Timeout performing RPUSH (5000ms), next: LLEN ROUTER_IEORALG9, inst: 1, qu: 0, qs: 35, aw: False, rs: ReadAsync, ws: Idle, in: 0, in-pipe: 0, out-pipe: 0, serverEndpoint: 192.168.37.182:8081, mgr: 10 of 10 available, clientName: IETRD-15628-2, IOCP: (Busy=1,Free=999,Min=8,Max=1000), WORKER: (Busy=18,Free=32749,Min=8,Max=32767), v: 2.0.601.3402 (Please take a look at this article for some common client-side issues that can cause timeouts: https://stackexchange.github.io/StackExchange.Redis/Timeouts)
03:29:26.42 :: Stack Trace :: at StackExchange.Redis.ConnectionMultiplexer.ExecuteSyncImpl[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\ConnectionMultiplexer.cs:line 2239
at StackExchange.Redis.RedisBase.ExecuteSync[T](Message message, ResultProcessor`1 processor, ServerEndPoint server) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisBase.cs:line 54
at StackExchange.Redis.RedisDatabase.ListRightPush(RedisKey key, RedisValue value, When when, CommandFlags flags) in C:\projects\stackexchange-redis\src\StackExchange.Redis\RedisDatabase.cs:line 1004
at BL.RedisConnection.InsertIntoList(Byte[] messageToSend, Boolean isLoginRequest, String loginID)
此异常会停止提取数据,对于不同的用户,它会变得非常随机。。像有些人持续到2-3分钟,有些人可能在30-40秒后停止。以前,它是同时发生在所有连接的用户在同一时间,但现在发生在随机时间与随机数的用户。大约有5-10个应用程序在这些redis端口连接。我们使用单一列表来插入服务器应用程序读取的数据&每个连接的用户使用单独的列表来获取数据。
我试着阅读和实现异常细节下的链接中提到的选项,但没有成功。请说明我做错了什么?是实施上还是出了什么问题?
当做,
霍尼亚舒
暂无答案!
目前还没有任何答案,快来回答吧!