I was wondering if it is possible to implement a form of Leaky Bucket, I would like to use Hangfire to implement multiple types of Background Jobs, however for a specific type of job (calls to an external API), I need to limit the time in between calls. Does anyone have any thoughts on how I could rate limit jobs of a specific type?
I would like to queue all my calls to the API into the Hangfire, and then limit their execution, possibly by a check if limited, then retry (which sounds perfect for Hangfires execute at least once)
It’s hard to tell what you’re trying to accomplish with the leaky bucket pattern and recommend a solution with the level of detail you’ve given here.
For delaying work, we generally implement a pattern in our jobs to check to see if it’s ok for that job to do work, and if we need to try again later, that job will queue itself up with a delay (i.e. run again in 5 minutes). The determination of whether that job should execute could be stored in a database on your side or passed as information into the subsequent requeue.
I was able to implement a global static class to Rate Limit. I’ll post the code later =D
This might be painfully obvious, but it’s worth stating…be careful. Mixing static state with multi-threaded applications (i.e. Hangfire) is dangerous, especially when using shared state. You’ll have to take care to ensure the data is synchronized between threads. http://stackoverflow.com/questions/3037637/c-sharp-what-if-a-static-method-is-called-from-multiple-threads