upstash / ratelimit-js

Rate limiting library for serverless runtimes
https://ratelimit-with-vercel-kv.vercel.app
MIT License
1.65k stars 33 forks source link

Limiting Concurrency #71

Closed bkniffler closed 5 months ago

bkniffler commented 10 months ago

Thanks for this library! Would it be hard to add limitting concurrency? Would love to use this library with upstash redis to limit API calls, but a few APIs incur concurrency/parallelity limitations. Currently using bottleneck.

ogzhanolguncu commented 9 months ago

Could you please give some more details about what you are trying to achieve?

bkniffler commented 9 months ago

Lets say I want to call API a, which has these limitations:

Here is an example with Bottleneck:

const limiter = new Bottleneck({
  reservoir: 100, // initial value
  reservoirRefreshAmount: 100,
  reservoirRefreshInterval: 60 * 1000,
  maxConcurrent: 2,
});

With upstash ratelimit:

const ratelimit = new Ratelimit({
  redis: Redis.fromEnv(),
  limiter: Ratelimit.slidingWindow(100, "60 s"),
});

But we're missing the concurrency part to limit this to 2 maximum requests in parallel. Does this make sense @ogzhanolguncu ?

ogzhanolguncu commented 9 months ago

I believe, without locking we can't achieve that right now. We are about to release a distributed lock soon. Maybe we can couple it with this and allow users to limit the concurrent access.

bkniffler commented 9 months ago

Thanks @ogzhanolguncu, Sounds cool, is there any info about the distributed lock already?

ogzhanolguncu commented 9 months ago

We released it yesterday, here you go. https://github.com/upstash/lock

github-actions[bot] commented 8 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

ogzhanolguncu commented 8 months ago

bump

github-actions[bot] commented 7 months ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 30 days.

github-actions[bot] commented 5 months ago

This issue was closed because it has been stalled for 30 days with no activity.

bkniffler commented 2 months ago

I've just tried to use the ratelimitter of up stash again. lock doesn't really help because it neither allows to wait for until the lock is resolved nor does it allow to set a concurrency @ogzhanolguncu :(

ogzhanolguncu commented 2 months ago

We are planning to add a native support for concurrency pretty soon.

bkniffler commented 2 months ago

Great, would love to be pinged. I've implemented Bottleneck for now. Happy to switch to ratelimit-js once concurrency works well.