Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limiter not working at all? Followed documentation. #292

Closed
2 tasks done
Dmitriyx opened this issue Apr 22, 2023 · 3 comments
Closed
2 tasks done

Rate limiter not working at all? Followed documentation. #292

Dmitriyx opened this issue Apr 22, 2023 · 3 comments

Comments

@Dmitriyx
Copy link

Dmitriyx commented Apr 22, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.15

Plugin version

8.0

Node.js version

18

Operating system

MacOS

Operating system version (i.e. 20.04, 11.3, 10)

Macbook Pro M2 Chip
Ventura 13.0

Description

Start the server and try to hit the rate limit. Seems like it is not working. I followed the documentation and even made a separate project to test this.

Steps to Reproduce

Package JSON:

{
  "name": "testapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@fastify/rate-limit": "^8.0.0",
    "fastify": "^4.15.0"
  }
}

server.js

// Require the framework and instantiate it
const fastify = require('fastify')({ logger: true })

fastify.register(require('@fastify/rate-limit'), {
    global: true,
    max: 1,
    timeWindow: '1 hour',
})

// Declare a route
fastify.get('/', async (request, reply) => {
    return { hello: 'world' }
})

// Run the server!
const start = async () => {
    try {
        await fastify.listen({ port: 3000 })
    } catch (err) {
        fastify.log.error(err)
        process.exit(1)
    }
}
start()

Expected Behavior

exptected to hit rate limit after starting server and spamming GET expoint, but what happens is that I get status code 200.

I tried to tinker with this many times and I do not have any success.

image
Source: https://github.com/fastify/fastify-rate-limit

@Eomm
Copy link
Member

Eomm commented Apr 22, 2023

Your code on the left is not the same as the doc code on the right.

image

A necessary await is missing in front of fastify.register(plugin)

@Dmitriyx
Copy link
Author

thanks for the quick response.
I thought perhaps it would still work since the routes registered correctly ( in my other project ).
I will test with top level await and have an update here.

@Dmitriyx
Copy link
Author

works! thanks for the help

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

No branches or pull requests

2 participants