Skip to content

Commit 19bf63c

Browse files
committed
Add support for maxTimeMS mongoOption
1 parent 2fb4f89 commit 19bf63c

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed

spec/MongoStorageAdapter.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,38 @@ describe_only_db('mongo')('MongoStorageAdapter', () => {
6060
});
6161
});
6262

63+
it('find succeeds when query is within maxTimeMS', (done) => {
64+
const maxTimeMS = 100;
65+
let adapter = new MongoStorageAdapter({
66+
uri: databaseURI,
67+
mongoOptions: { maxTimeMS },
68+
});
69+
adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
70+
.then(() => adapter._rawFind('Foo', { '$where': `sleep(${maxTimeMS / 2})` }))
71+
.then(() => done());
72+
})
73+
74+
it('find fails when query exceeds maxTimeMS', (done) => {
75+
const maxTimeMS = 100;
76+
let adapter = new MongoStorageAdapter({
77+
uri: databaseURI,
78+
mongoOptions: { maxTimeMS },
79+
});
80+
adapter.createObject('Foo', { fields: {} }, { objectId: 'abcde' })
81+
.then(() => adapter._rawFind('Foo', { '$where': `sleep(${maxTimeMS * 2})` }))
82+
.then(
83+
() => {
84+
done('Find succeeded despite taking too long!');
85+
},
86+
(err) => {
87+
expect(err.name).toEqual('MongoError');
88+
expect(err.code).toEqual(50);
89+
expect(err.message).toEqual('operation exceeded time limit');
90+
done();
91+
}
92+
);
93+
});
94+
6395
it('stores pointers with a _p_ prefix', (done) => {
6496
let obj = {
6597
objectId: 'bar',

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default class MongoCollection {
1313
// none, then build the geoindex.
1414
// This could be improved a lot but it's not clear if that's a good
1515
// idea. Or even if this behavior is a good idea.
16-
find(query, { skip, limit, sort, keys } = {}) {
17-
return this._rawFind(query, { skip, limit, sort, keys })
16+
find(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
17+
return this._rawFind(query, { skip, limit, sort, keys, maxTimeMS })
1818
.catch(error => {
1919
// Check for "no geoindex" error
2020
if (error.code != 17007 && !error.message.match(/unable to find index for .geoNear/)) {
@@ -30,22 +30,29 @@ export default class MongoCollection {
3030
index[key] = '2d';
3131
return this._mongoCollection.createIndex(index)
3232
// Retry, but just once.
33-
.then(() => this._rawFind(query, { skip, limit, sort, keys }));
33+
.then(() => this._rawFind(query, { skip, limit, sort, keys, maxTimeMS }));
3434
});
3535
}
3636

37-
_rawFind(query, { skip, limit, sort, keys } = {}) {
37+
_rawFind(query, { skip, limit, sort, keys, maxTimeMS } = {}) {
3838
let findOperation = this._mongoCollection
3939
.find(query, { skip, limit, sort })
4040

4141
if (keys) {
4242
findOperation = findOperation.project(keys);
4343
}
44+
45+
if (maxTimeMS) {
46+
findOperation = findOperation.maxTimeMS(maxTimeMS);
47+
}
48+
4449
return findOperation.toArray();
4550
}
4651

47-
count(query, { skip, limit, sort } = {}) {
48-
return this._mongoCollection.count(query, { skip, limit, sort });
52+
count(query, { skip, limit, sort, maxTimeMS } = {}) {
53+
let countOperation = this._mongoCollection.count(query, { skip, limit, sort, maxTimeMS });
54+
55+
return countOperation;
4956
}
5057

5158
insertOne(object) {

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export class MongoStorageAdapter {
9292
this._uri = uri;
9393
this._collectionPrefix = collectionPrefix;
9494
this._mongoOptions = mongoOptions;
95+
96+
// MaxTimeMS is not a global MongoDB client option, it is applied per operation.
97+
this._maxTimeMS = mongoOptions.maxTimeMS;
98+
delete mongoOptions.maxTimeMS;
9599
}
96100

97101
connect() {
@@ -329,7 +333,13 @@ export class MongoStorageAdapter {
329333
return memo;
330334
}, {});
331335
return this._adaptiveCollection(className)
332-
.then(collection => collection.find(mongoWhere, { skip, limit, sort: mongoSort, keys: mongoKeys }))
336+
.then(collection => collection.find(mongoWhere, {
337+
skip,
338+
limit,
339+
sort: mongoSort,
340+
keys: mongoKeys,
341+
maxTimeMS: this._maxTimeMS,
342+
}))
333343
.then(objects => objects.map(object => mongoObjectToParseObject(className, object, schema)))
334344
}
335345

@@ -358,14 +368,18 @@ export class MongoStorageAdapter {
358368

359369
// Used in tests
360370
_rawFind(className, query) {
361-
return this._adaptiveCollection(className).then(collection => collection.find(query));
371+
return this._adaptiveCollection(className).then(collection => collection.find(query, {
372+
maxTimeMS: this._maxTimeMS,
373+
}));
362374
}
363375

364-
// Executs a count.
376+
// Executes a count.
365377
count(className, schema, query) {
366378
schema = convertParseSchemaToMongoSchema(schema);
367379
return this._adaptiveCollection(className)
368-
.then(collection => collection.count(transformWhere(className, query, schema)));
380+
.then(collection => collection.count(transformWhere(className, query, schema), {
381+
maxTimeMS: this._maxTimeMS,
382+
}));
369383
}
370384

371385
performInitialization() {

0 commit comments

Comments
 (0)