Skip to content

Commit

Permalink
S3UTILS-157: explicitly close the mongodb cursors
Browse files Browse the repository at this point in the history
  • Loading branch information
williamlardier committed Mar 29, 2024
1 parent 5b5a763 commit a6df332
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions utils/S3UtilsMongoClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ const __COUNT_ITEMS = 'countitems';

class S3UtilsMongoClient extends MongoClientInterface {
async getObjectMDStats(bucketName, bucketInfo, isTransient, log, callback) {
let cursor;
try {
const c = this.getCollection(bucketName);
const cursor = c.find({}, {
cursor = c.find({}, {
projection: {
'_id': 1,
'value.last-modified': 1,
Expand All @@ -44,11 +45,15 @@ class S3UtilsMongoClient extends MongoClientInterface {

const locationConfig = getLocationConfig(log);

const usersBucketCreationDatesArray = await this.getCollection(USERSBUCKET).find({}, {
const cursorUsersBucketCreationDates = await this.getCollection(USERSBUCKET).find({}, {
projection: {
'value.creationDate': 1,
},
}).toArray();
});

const usersBucketCreationDatesArray = cursorUsersBucketCreationDates.toArray();

await cursorUsersBucketCreationDates.close();

const usersBucketCreationDatesMap = usersBucketCreationDatesArray
.reduce((map, obj) => ({ ...map, [obj._id]: obj.value.creationDate }), {});
Expand Down Expand Up @@ -196,6 +201,14 @@ class S3UtilsMongoClient extends MongoClientInterface {
errorString: err.toString(),
});
return callback(err);
} finally {
if (cursor && !cursor.closed) {
log.info('Finished processing cursor', {
method: 'getObjectMDStats',
bucketName,
});
cursor.close();
}
}
}

Expand Down

0 comments on commit a6df332

Please sign in to comment.