MeVitae / redis-work-queue

A cross-platform work queue, on top of a redis database, with implementations in Python, Rust, Go, Node.js (TypeScript) and Dotnet (C#).
MIT License
15 stars 2 forks source link

Support rate-limited item insertion #9

Open JOT85 opened 3 months ago

JOT85 commented 3 months ago

This was requested by @nathanwl88

I propose adding a wrapper around the work queue that would have essentially the same methods, except that the method for adding an item takes both an item and a new rate limiting tag (a string).

Each rate limiting tag would have an optional rate limit (in items per second, items per minute, or some similar unit).

If the rate of item insertion exceeds the limit, new items would be placed in a separate queue, and only added into the main queue at the rate specified by the rate limit.

Example

Let's suppose there are two rate limiting tags, foo (with a rate limit of 10 items per minute) and bar (with a rate limit of 20 items per minute).

In the first minute, if 15 items are added with tag foo, and a further 30 with tag bar, then the first 10 with the tag foo would enter the main queue, along with the first 20 with the tag `bar.

Then, in the second minute, the overflowing items from the previous minute are added. If 10 items were added with the foo tag, 5 of those would end up in the main queue (after those from the first minute).

Finally, in the third minute, the final 5 items with the foo tag would be added to the main queue.

JOT85 commented 3 months ago

@nathanwl88 would that do everything you need it to?

nathanwl88 commented 3 months ago

@JOT85 this sounds perfect