fastify-rate-limit icon indicating copy to clipboard operation
fastify-rate-limit copied to clipboard

Support rate limit groups for specific routes

Open OleksandrBoiko1 opened this issue 1 year ago • 4 comments

Prerequisites

  • [X] I have written a descriptive issue title
  • [ ] I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

It'll be great to have an option to apply rate limit for this two endpoints, not for the whole project. Something that required here. Because solution from issues is not working

Important notice, I mean that rateLimit should calculate calls for this two endpoints, not separate

const routes: FastifyPluginAsync = async function (f) {
  const fastify = f.withTypeProvider<ZodTypeProvider>();

  fastify.post(
    '/',
    {
      schema: {
        tags: SCHEMA_TAGS,
        body: BodySchema,
        response: {
          200: ResponseSchema
        }
      }
    },
    async (req) => {
      // Doing something
    }
  );

  fastify.get(
    '/',
    {
      schema: {
        tags: SCHEMA_TAGS,
        response: {
          200: ResponseSchema
        }
      }
    },
    async (req) => {
      // Doing something
    }
  );
};

export default routes;

Thanks!

Motivation

Usefull feature!

Example

Register fastifyRateLimit only for encapsulate endpoints fastify.register(fastifyRateLimit, { config: { rateLimit: { max: 3, timeWindow: '1 minute' } } })

OleksandrBoiko1 avatar Jun 27 '24 19:06 OleksandrBoiko1

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

mcollina avatar Jul 01 '24 10:07 mcollina

Hi @mcollina I am able to implement this feature for Redis as a store but not for LRU Cache. Need some help.

aniketcodes avatar Aug 03 '24 16:08 aniketcodes

Why? What's the problem? An in-memory implementation should be easier.

mcollina avatar Aug 03 '24 17:08 mcollina

In Redis, It is easier to modify the key. As per my understanding for LRU cache, different instance of cache is being created for each route. Incase of redis, same redis instance is being used.

aniketcodes avatar Aug 03 '24 17:08 aniketcodes

Its done

aniketcodes avatar Sep 02 '24 17:09 aniketcodes