Hi, I'm looking for a rate limiter library suitabl...
# arrow
f
Hi, I'm looking for a rate limiter library suitable for kotlin multiplatform. I'm currently using Bucket4k but it is JVM based. A very simple token bucket algorithm would be sufficient. Is that something arrow can help with?
s
Hey @Francis Reynders, We've considered building something like a rate limiter, but since it requires persistence to be compatible distributed environments it's outside of the scope of Arrow. That being said, whatever you're using Arrow for should be compatible with bucket4k so you can easily and safely mix both APIs. If you for example wanted to combine it with
Schedule
, or
CircuitBreaker
.
f
Hi @simon.vergauwen, thanks for the quick response. Understood, my use case is not distributed though, will have a look at possibly implementing myself.
s
A naive implementation could look like this. Not sure if that helps 😅
Untitled.cpp
f
Thank you very much for the sample. Not exactly what I need but surely helps to illustrate Schedule. I don't need evenly spaced though, I want to maximize concurrent execution within the limits of the rate limit; suspending while waiting for next execution allowance within these limits. The bucket token algorithm is doing that as far as I understand.
s
My pleasure! You should be able to achieve that with the
buffer
though, and this example should be easily updatable to that. This encoding is actually simpler. Let's say your rate limit allows 100 calls per second you can do.
val limiter = Limiter(1.second, 100)
If you think this is useful we can add more complete like this to Arrow Resilience, for the cases that don't require something as complex as bucket4k. I've discussed this with @Alejandro Serrano.Mena in the past if I'm not mistaken 🤔
🙌 1