Skip to content

Instantly share code, notes, and snippets.

@chandelabhishek
chandelabhishek / rate-limiter.js
Created May 3, 2024 06:50
sliding window rate limter using redis and nodejs
/**
* parameters for both functions:
* 1. isOverTheLimit
* 2. addRequestToRateLimitWndow
*
* is a key, which represents the key on which the rate limiter should be working.
* e.g.
* 1. if you want to rate limit a specific api, say, /v1/api then key is simply : `/v1/api`
* 2. if you want to rate limit a specific api and IP address, then key can a combination of both viz.: `/v1/api:34.10.43.23`
*
@chandelabhishek
chandelabhishek / aggrgate-query.js
Last active January 19, 2025 14:31
batch queries and run it in a single transaction - postgres, nodejs, knex
const pgp = require('pg-promise')();
pgp.pg.types.setTypeParser(20, parseInt);
function AggregateQuery(queryBuilders) {
// pass in a knex transaction
async function run(transaction) {
const queries = queryBuilders.map(qb => {
const { sql: query, bindings: values } = qb.toSQL().toNative();
return { query, values };
});