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

Using RateLimiterPostgres + knex.js yields wrong data in db #95

Closed
noamackerman-monday opened this issue Dec 14, 2020 · 3 comments
Closed
Labels
bug Something isn't working

Comments

@noamackerman-monday
Copy link

noamackerman-monday commented Dec 14, 2020

Hi,
When using the RateLimiterPostgres + knex.js, i'm seeing wrong results written to the db when the points limit is exceeded.

Code:
const {RateLimiterPostgres} = require('rate-limiter-flexible');
let options = { storeClient: connection, storeType: knex, points: 2, duration: 0, blockDuration: 60 * 60 * 24 * 365, tableName: 'downloads_rate_limiter', keyPrefix: 'downloadsrl', inmemoryBlockOnConsumed: 2, inmemoryBlockDuration: 60 * 60 * 24 * 365, tableCreated: true };

const rateLimiter = new RateLimiterPostgres(options);

app.get('/hello', async (req, res) => {
try { await rateLimiter.consume(obj, 1); doSomeOperation(); } catch (error) { if (error instanceof Error) { // Some Postgres error // Never happen ifinsuranceLimiter set up res.writeHead(500) } else { // Can't consume res.writeHead(429); } res.end(error.toString()) }
});

Now let's say i'm calling the 'hello' endpoint 3 times and looking the at points column in the db. After the first 2 times i will see the value 2, which is expected, but at the 3rd time and on i will start to see some random value, jumping from 2 to 6, then to 9 and 12 etc.

Can you please help me understand what is the reason for this behaviour? Seems like a bug.

Thanks!
Noam

@animir animir added the bug Something isn't working label Dec 15, 2020
@animir
Copy link
Owner

animir commented Dec 15, 2020

@noamackerman-monday Hi, it looks like a bug. I am quite busy now, I am going to look at it over the next weekend. If you need a fix earlier, you can try search a bug from this block of code

if (this.blockDuration > 0 && res.consumedPoints <= (this.points + changedPoints)) {

@animir
Copy link
Owner

animir commented Dec 19, 2020

@noamackerman-monday I fixed this bug and published patched 2.1.15 version to npm.

Why do you need to know consumedPoints after block, btw? Just curious about your use case.

@animir animir closed this as completed Dec 19, 2020
@noamackerman-monday
Copy link
Author

@animir Amazing thanks!
I don't need to know it actually, just did some basic testing to see that it works as expected and found it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants