Reputation: 2609
I have an ASP.Net MVC 4 application that periodically calls an external API for information (resource). This resource has a rate limiter for the account (meaning other apps use the same pool and may hit the limit). When this limit is hit, It will send back a HTTP Status Code 429 with a header of "Retry-After" in seconds (lets say 25 seconds).
If my app gets this response, I will then need to delay execution for 25 Seconds and retry. First off, let me say that the method that this code is running under is an ASP.Net 4.5 Async method. For this, I was thinking about using the System.Threading.Thread.Sleep(25000)
Now, I really don't like to use this, is there a better way of doing this?
I have to say that I apologize for this open ended question, but I couldn't find anything on the proper way of delay execution (while keeping things async and making sure that we don't run out of threads)
Update: Would the following code be better for the delay?
await Task.Run(() => Thread.Sleep(10000))
Upvotes: 9
Views: 13281
Reputation: 37957
You should use neither Sleep() nor Delay().
I would build a class that accepts multithreaded requests and responds in a timely way while providing single-threaded, controlled access to the Web API. The Web API is too slow for the sub-second expected runtime of a page request.
Determine how stale data can be and still be acceptable, and build a cache of the data on your end that consistently responds in milliseconds not seconds.
Upvotes: 0
Reputation: 61666
25 seconds is a lot of time to wait for the response on the client-side, while you're delaying the 3rd party API request on the server. It might be a good idea to initiate the retry from the client AJAX code, if you really need this data/resource on the client-side. Then you wouldn't have to organize the wait on the server-side at all.
Upvotes: 3
Reputation: 456457
I recommend you use the Transient Fault Handling Block, which was designed specifically for this kind of scenario.
However, if you want to implement your own retry mechanism, Task.Delay
is a better choice than Task.Run
+Thread.Sleep
.
Upvotes: 7
Reputation: 2562
Since it appears you are doing this in ASP.NET, then you could use Quartz.net to schedule jobs and change when a job needs to execute again (say 25 seconds). Their website: Quartz Enterprise Scheduler .NET.
A previous question on using Quartz.net in ASP.NET is located here. Hope that helps you out.
Upvotes: 1
Reputation: 116548
You shouldn't use Thread.Sleep
because it blocks the thread for that amount of time so your server is less scalable. You should instead use Task.Delay
which waits asynchronously without blocking a thread:
await Task.Delay(10000)
Task.Delay
uses a Timer
internally to accomplish that. For more info: Thread.Sleep vs Task.Delay?
Upvotes: 16