How to implement a ServiceStack Redis Client with a timeout

We implement a template in which our client checks to see if a document exists in Redis, and if not, we retrieve data from the database.

We are trying to handle the case when the Redis server is unavailable or unavailable, so we can immediately extract from the database.

However, when we test our code by intentionally deleting the Redis server, the Redis call through the ServiceStack client does not expire after 20 seconds.

We tried to use the RedISClient.SendTimeout property for various values ​​(1000, 100, 1), but the timeout always occurs after about 20 seconds. We also tried using the .Ping () method, but had the same problem.

Question: how can we handle the scenario when the Redis server is disconnected and we want to switch to the database selection faster?

+6
source share
2 answers

I had a similar problem with sending by email: sometimes there is no answer, and the assembly timeout (from SmtpClient) does nothing. In the end, I get a timeout that I assume comes from the basic TCP / IP layer. I set the timeout in the client a little shorter than the "cruel timeout" in Task.Wait.

My solution was to wrap the call in Task and use a timeout on this:

// this special construct is to set a timeout (the SmtpClient timeout does not seem to work) var task = Task.Factory.StartNew(() => SendEmail(request)); if (!task.Wait(6000)) Log.Error("Could not send mail to {0}. Timeout (probably on TCP layer).".Fmt(request.To)); 

Maybe something like this will work for you, just replace SendEmail with the method that does the Redis thing.

+1
source

You should not rely on the redis server to tell you how long the request should wait before moving to plan B. Put this logic in the code that executes the request so that it does not depend on how the redis server is configured

0
source

Source: https://habr.com/ru/post/911246/


All Articles