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

APIs to reset consumed tokens and get remaining tokens #84

Closed sourabpramanik closed 5 months ago

sourabpramanik commented 6 months ago

This PR adds two new APIs:

Feat: #80

vercel[bot] commented 6 months ago

@sourabpramanik is attempting to deploy a commit to the Upstash Team on Vercel.

A member of the Team first needs to authorize it.

ogzhanolguncu commented 6 months ago

Looks good, and let's add a reset button to next.js example

sourabpramanik commented 6 months ago

For the get remaining API, do you guys have any suggestions for the implementation? I am trying to figure out a way where every algorithm will have their own method to retrieve the remaining tokens because the data type for each algorithm is different. So please let me know if you have any better ideas.

ogzhanolguncu commented 6 months ago

I believe that as long as the commands remains in Lua script, it should be acceptable. We don't want to cause more round-trips than necessary.

sourabpramanik commented 6 months ago

@ogzhanolguncu @enesakar These are my proposed changes, the implementation of the algorithm does not change just that I have changed the Algorithm function return signature so that we can add get remaining tokens API for each different window as they use different methods to store tokens in Redis.

Changes applied to single region fixed window algorithm

ogzhanolguncu commented 6 months ago
const ratelimit = new Ratelimit({
  redis: kv,
  limiter: {getRemaining: _,limit: _},
});

Why do we expose getRemaining and limit to outside when they can't even initialize it that way?

I think initializer config signature should be the same. It's a bit confusing both for the users and devs. Imagine firing up your intellisense and seeing getRemaining there, but you can't do anything, I believe it's just confusing.

We should find another way to append getRemaining to ratelimit instance

sourabpramanik commented 6 months ago

The signature doesn't have to be like this it can work like it used to do by providing the limiter function. I will refactor this

sourabpramanik commented 6 months ago
const ratelimit = new Ratelimit({
  redis: kv,
  limiter: {getRemaining: _,limit: _},
});

Why do we expose getRemaining and limit to outside when they can't even initialize it that way?

I think initializer config signature should be the same. It's a bit confusing both for the users and devs. Imagine firing up your intellisense and seeing getRemaining there, but you can't do anything, I believe it's just confusing.

We should find another way to append getRemaining to ratelimit instance

Yeah I actually missed the intellisense 😅

sourabpramanik commented 6 months ago
const ratelimit = new Ratelimit({
  redis: kv,
  limiter: {getRemaining: _,limit: _},
});

Why do we expose getRemaining and limit to outside when they can't even initialize it that way?

I think initializer config signature should be the same. It's a bit confusing both for the users and devs. Imagine firing up your intellisense and seeing getRemaining there, but you can't do anything, I believe it's just confusing.

We should find another way to append getRemaining to ratelimit instance

@ogzhanolguncu How about this implementation?

ogzhanolguncu commented 6 months ago

APIs are definitely much better. I like it

sourabpramanik commented 5 months ago

Thanks a lot for the changes! I think the changes look good overall. I only made a small fix in 4c9002d.

Thanks @CahidArda

sourabpramanik commented 5 months ago

I feel like we can leave the discussion about changing the API for another time.

Yes I believe we can create a new issue for the same and discuss it throughout