You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We use RateLimiterMongo on a server running on AWS lambda.
Since RateLimiterMongocallscreateIndex on creation, it results in calling createIndex for every lambda invocation that is a cold start.
This has two issues:
Causes lambda to crash with UnhandledPromiseRejection, for example
Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"MongoError: Pool was force destroyed","reason":{"errorType":"MongoError","errorMessage":"Pool was force destroyed","name":"MongoError","stack":["MongoError: Pool was force destroyed"," at Pool.destroy (/var/task/node_modules/mongodb/lib/core/connection/pool.js:679:21)"," at Server.destroy (/var/task/node_modules/mongodb/lib/core/topologies/server.js:902:15)"," at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/core/topologies/replset.js:357:26)"," at listOnTimeout (internal/timers.js:554:17)"," at processTimers (internal/timers.js:497:7)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: MongoError: Pool was force destroyed"," at process.<anonymous> (/var/runtime/index.js:35:15)"," at process.emit (events.js:326:22)"," at process.EventEmitter.emit (domain.js:483:12)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
This is because createIndex is called without waiting for the result. If the its called in one lambda call and it finished before its done it will throw in the following lambda invocation.
Performance
I would recommend:
Adding option to choose if createIndex should be called at all
Add a method public method to call createIndex with async
Catch errors from the createIndex calls in _initCollection with rethrow. It's very difficult to find the reason for the UnhandledPromiseRejection otherwise.
This is what we currently use to alleviate the issue:
/** * Rate limiter that doesn't create indexes on initialize * Instead we create the indexes once on init */exportclassRateLimiterMongooseextendsRateLimiterMongo{publicreadonlytableName: string;protectedclient: Connection;protected_collection: Collection/** * Override _initCollection and don't call createIndex * * Source: https://github.com/animir/node-rate-limiter-flexible/blob/v2.2.1/lib/RateLimiterMongo.js#L78 */protected_initCollection(): void{this._collection=this.client.collection(this.tableName);}/** * Create the indexes required, but unlike the original _initCollection, use await to catch for errors */publicasyncinitIndexes(): Promise<void>{constcollection=this.client.collection(this.tableName);awaitcollection.createIndex({expire: -1},{expireAfterSeconds: 0});awaitcollection.createIndex(Object.assign({},this.indexKeyPrefix,{key: 1}),{unique: true});}}
The text was updated successfully, but these errors were encountered:
animir
changed the title
Add option not to create indexes on RateLimiterMongo (a must for for serverless environments)
RateLimiterMongo: add option not to create indexes (a must for for serverless environments)
Oct 30, 2021
We use
RateLimiterMongo
on a server running on AWS lambda.Since
RateLimiterMongo
callscreateIndex
on creation, it results in calling createIndex for every lambda invocation that is a cold start.This has two issues:
This is because createIndex is called without waiting for the result. If the its called in one lambda call and it finished before its done it will throw in the following lambda invocation.
I would recommend:
This is what we currently use to alleviate the issue:
The text was updated successfully, but these errors were encountered: