使用 StackExchange.Redis 处理瞬时网络错误 [英] Handling of transient network errors with StackExchange.Redis

查看:99
本文介绍了使用 StackExchange.Redis 处理瞬时网络错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找有关在使用 StackExchange.Redis 客户端时处理临时网络问题的指南.

Looking for guidance on handling temporary network issues when using the StackExchange.Redis client.

为了澄清起见,我并不是说最初使用 ConnectionMultiplexer.Connect() 连接到 Redis 服务器.我指的是如何处理在操作过程中因任何原因而中断的连接.(我们可以假设命令是幂等的,StringSet,StringGet)

For clarification, I do not mean initially connecting to the Redis server with ConnectionMultiplexer.Connect(). I am referring to how to handle connections that are interrupted for whatever reason in the middle of an operation. (we can assume that the command is idempotent, StringSet, StringGet)

我这么问是因为我们正在从 ServiceStack 迁移到 StackExchange 客户端.在我们要替换的使用 ServiceStack 的代码中,异常被捕获,并且会在一个短的 thread.sleep() 之后再次尝试该操作.这种情况在我们的生产环境中经常发生,并且在大多数情况下重试会奏效.

I ask because we are migrating from the ServiceStack to the StackExchange client. In the code we are replacing, which uses ServiceStack, exceptions were caught and the operation would be attempted again after a short thread.sleep(). This happens fairly frequently in our production environment and on most occasions the retry would work.

通常它是一个 System.Net.SocketException,带有消息已建立的连接被主机中的软件中止"或现有连接被远程主机强行关闭"

Usually it's a System.Net.SocketException with the message "An established connection was aborted by the software in your host machine" or "An existing connection was forcibly closed by the remote host"

如果抛出 System.Net.SocketException,StackExchange.Redis 是否会自动重试直到超过 syncTimeout 时间?

If a System.Net.SocketException is thrown, does StackExchange.Redis automatically retry up until the syncTimeout time has elapsed?

如果 SE.Redis 没有自动重试,在我们的代码中,在初始操作失败和重试之间是否有任何建议的步骤?如:

If SE.Redis does not automatically retry, are there any suggested steps that should happen between an initial operation failure and a retry in our code? Such as:

  • 等待很短的时间?
  • 重新创建多路复用器?(我猜不会)
  • 调用 Close() 和 Configure()?

感谢您的指导.

推荐答案

它不会自动重试.如果发生错误,您需要自己捕获并重试.

It doesn't retry automatically. If an error occurs, you need to catch and retry yourself.

就实际重试而言,只要多路复用器的一些连接仍然有效,您就可以退后重试.

In terms of the actual retry, as long as some of the multiplexer's connections are still alive, you can just back off and try again.

如果连接失败,它尝试恢复,但有一个长期存在的错误,它可以间歇性地进入一些不可恢复的状态并保持断开连接.如果 IsConnected 曾经返回 false,我们会通过重新创建多路复用器来解决这个问题.

If a connection fails, it tries to recover, but there's a long standing bug where it can intermittently get into some unrecoverable state and remain disconnected. We work around this by re-creating the multiplexer if IsConnected ever returns false.

这篇关于使用 StackExchange.Redis 处理瞬时网络错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆