Skip to content

Add function to manually check rate limit #346 #408#410

Closed
umakantp wants to merge 3 commits intofastify:masterfrom
umakantp:master
Closed

Add function to manually check rate limit #346 #408#410
umakantp wants to merge 3 commits intofastify:masterfrom
umakantp:master

Conversation

@umakantp
Copy link
Copy Markdown

@umakantp umakantp commented Jan 19, 2025

This is the updated PR (as I did not have access to the original PR) copy of 392. It is a copy with an added test case on work done by @Charioteer and uses the latest master. I did not change any code. Just did some testing and added a unit test case.

The reason for finishing up the PR is issue no 408. I'm copying the rest of the description here written by @Charioteer so everything stays in one place. Feel free to close if you can provide me access to the original branch to make the change & wrap it up.

I can add the documentation given if this PR looks good to the contributors.

From @Charioteer :

Following on from issue #346 and my comment, I finally had some spare time to implement a draft version of my idea as suggested by @mcollina. I realized that any store (child) instance receives all the options provided to fastify.rateLimit when it is constructed, which is why I had to adjust my initial idea and come up with a slightly different solution.

Thanks and cheers Patrick

How to Use

import Fastify from "fastify";
import { fastifyRateLimit } from "@fastify/rate-limit";

const fastify = Fastify();

await fastify.register(fastifyRateLimit, {
  global: false,
  max: 10,
  timeWindow: "10 seconds",
});

const checkRateLimit = fastify.createRateLimit(); // this will use the global options provided to fastifyRateLimit

fastify.get("/", async (request, reply) ={
  const limit = await checkRateLimit(request);

  if(!limit.isAllowed && limit.isExceeded) {
    return reply.code(429).send("Limit exceeded");
  }

  return reply.send("Hello world");
});


const checkCustomRateLimit = fastify.createRateLimit({ max: 100 }); // max provided to override global options

fastify.get("/custom", async (request, reply) ={
  const limit = await checkCustomRateLimit(request);

  if(!limit.isAllowed && limit.isExceeded) {
    return reply.code(429).send("Limit exceeded");
  }

  return reply.send("Hello world");
});

Checklist

@umakantp umakantp marked this pull request as draft January 19, 2025 06:07
@mcollina
Copy link
Copy Markdown
Member

Why is still in draft?

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use tsd to tests our types.

@Charioteer
Copy link
Copy Markdown
Contributor

Hi,

thanks @umakantp for completing/continuing my PR #392. As you may have noticed, I've been busy the whole time and didn't find any spare time yet. I was hoping to complete it in the 1st or 2nd week of February. Regardless of whether you finalize this “new” PR now, I would be very grateful if the authorship of my original code would remain visible in the commit history, which is currently not the case with a merge of #410. But I don't know to what extent this is possible now (@mcollina ?) and in case of doubt it is the way it is, I had enough time to finish it myself and didn't 😄. I also don't think it's a good idea to continue working on #392 now, because it would - vise-versa - collide with your work.

@umakantp
Copy link
Copy Markdown
Author

Hi @Charioteer

I'm fine passing on original commits authorship to you (if possible). You are the owner, after all, and that is why I wrote for history and tagged you in this PR, so there is always your reference for ownership of the code to you. I'm fine even passing you ownership of this PR (if it also transfers the commit's ownership). All I care for is the feature to be released & hence took over to complete & get it out. So up to you and @mcollina, how you want to proceed with this.

@umakantp umakantp requested a review from mcollina January 28, 2025 15:22
@umakantp umakantp marked this pull request as ready for review January 31, 2025 08:25
@mcollina
Copy link
Copy Markdown
Member

@umakantp why dind't you use the original commits from @Charioteer ?

@umakantp
Copy link
Copy Markdown
Author

umakantp commented Feb 1, 2025

@umakantp why dind't you use the original commits from @Charioteer ?

  1. I was not able to use the original PR, since I did not had the edit access.
  2. If you mean by using the original commits means cherry-picking (or forking his branch to use)? Sorry that did not really strike me at that point.

Looks like this PR is more of problem than a solution for everyone. I'll close it. Will wait for anyone else willing to write a new code or @Charioteer to finish his original PR. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants