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

Version 1.1.2 throws errors #101

Closed ElectricCodeGuy closed 4 months ago

ElectricCodeGuy commented 4 months ago

After updating to 1.1.2 i get:

Failed to record analytics [UpstashError: WRONGTYPE Operation against a key holding the wrong kind of value, command was: ["zincrby","mw:events:1713535200000",1,"{\"identifier\":\"127.0.0.1\",\"success\":true}"]] { name: 'UpstashError' }

Downgrading makes the error go away.

I run analytics inside my middleware

const ratelimit = new Ratelimit({
  redis: redis,
  analytics: true,
  limiter: Ratelimit.slidingWindow(10, '10 s'),
  prefix: 'mw'
});

` if (request.method === 'POST') { const ip = request.ip ?? '127.0.0.1'; const { success, limit, reset, remaining } = await ratelimit.limit(ip);

if (!success) {
  // Construct a response for blocked requests
  const blockedResponse = new Response('Too Many Requests', {
    status: 429, // HTTP status code for Too Many Requests
    headers: {
      'X-RateLimit-Limit': limit.toString(),
      'X-RateLimit-Remaining': remaining.toString(),
      'X-RateLimit-Reset': reset.toString(),
      'Content-Type': 'text/plain'
    }
  });

  return blockedResponse;
}

}`

CahidArda commented 4 months ago

Hi @ElectricCodeGuy,

This can happen the first hour you upgraded your version. This is because in analytics, we had a hashmap for every hour which will now be a sorted list after version 1.1.2

This means that when you upgrade to version 1.1.2, you won't be able to submit analytics for the first hour. Then you will be able to submit.

Additionally, once we update Upstash console, you won't be able to view the dashboard in the first 24 hours after updating ratelimit.

Is this transition okay on your side? We had to do this transition because the old format used for analytics didn't scale well.

CahidArda commented 4 months ago

we are updating the error message to explain the transition to the new version in https://github.com/upstash/ratelimit/pull/103

ElectricCodeGuy commented 4 months ago

Thanks for response. Yes totally fine :) You can close this!