fastify / fastify-rate-limit

A low overhead rate limiter for your routes
MIT License
505 stars 72 forks source link

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

Closed Dmitriyx closed 1 year ago

Dmitriyx commented 1 year ago

Prerequisites

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

works! thanks for the help