Skip to content

Commit a011a77

Browse files
committed
middleware thottling comments
1 parent 23c3e36 commit a011a77

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/middleware/index.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,41 @@ export function expressRateLimiter(
4646
const redisClient = connect(redisClientOptions); // Default port is 6379 automatically
4747
const rateLimiter = setupRateLimiter(rateLimiterAlgo, rateLimiterOptions, redisClient);
4848

49+
// Sort the requests by timestamps to make sure we process in the correct order
50+
// We need to store the request, response and next object so that the correct one is used
51+
// the function we return accepts the unique request, response, next objects
52+
// it will store these and then process them in the order in which they were received.
53+
// We can do an event listener that waits for the previous request in the queue to be finished
54+
// store the middleware in closure
55+
56+
// Catch the request
57+
// add this to the queue
58+
// proccess the oldest request in the queue
59+
// check if the queue is empty => if not process the next request
60+
// otherwise return
61+
// process restarts when the next request comes in
62+
63+
// so r1, r2 come in
64+
// r1, and r2 get processed with thin same frame on call stack
65+
// r2 call is done once r2 is added to the queue
66+
67+
const requestsInProcess: { [index: string]: Request[] } = {};
68+
69+
// return a throttled middleware. Check every 100ms? make this a setting?
70+
// how do we make sure these get queued properly?
71+
// store the requests in an array when available grab the next request for a user
72+
/**
73+
* Request 1 comes in
74+
* Start handling request 1
75+
* In the meantime reqeust 2 comes in for the same suer
76+
* Finish handling request 1
77+
* check the queue for this user
78+
* if it's empty we're done
79+
* it it has a request handle the next one
80+
*
81+
* Not throttling on time just queueing requests.
82+
*/
83+
4984
// return the rate limiting middleware
5085
return async (
5186
req: Request,
@@ -70,6 +105,7 @@ export function expressRateLimiter(
70105
*/
71106
// check for a proxied ip address before using the ip address on request
72107
const ip: string = req.ips ? req.ips[0] : req.ip;
108+
// requestsInProcess[ip] = true;
73109

74110
// FIXME: this will only work with type complexity
75111
const queryAST = parse(query);

test/middleware/express.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,16 @@ describe('Express Middleware tests', () => {
347347
});
348348

349349
test('Multiple queries that exceed token limit', async () => {
350+
const requests = new Array(5).fill(0);
351+
350352
for (let i = 0; i < 5; i++) {
351353
// Send 5 queries of complexity 2. These should all succeed
352-
await middleware(
353-
mockRequest as Request,
354-
mockResponse as Response,
355-
nextFunction
354+
requests.push(
355+
middleware(
356+
mockRequest as Request,
357+
mockResponse as Response,
358+
nextFunction
359+
)
356360
);
357361

358362
// advance the timers by 20 miliseconds for the next request

0 commit comments

Comments
 (0)