Skip to content

Commit 757954d

Browse files
committed
Added support for maxMessageSizeBytes if available (DRIVERS-1)
1 parent 7075f65 commit 757954d

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

HISTORY

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Uses setImmediate if on 0.10 otherwise nextTick for cursor stream
88
- Fixed race condition in Cursor stream (NODE-31)
99
- Fixed issues related to node 0.10 and process.nextTick now correctly using setImmediate where needed on node 0.10
10+
- Added support for maxMessageSizeBytes if available (DRIVERS-1)
1011

1112
1.2.13 2013-02-22
1213
-----------------

lib/mongodb/connection/connection.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ var Connection = exports.Connection = function(id, socketOptions) {
2424
// Connection parsing state
2525
//
2626
this.maxBsonSize = socketOptions.maxBsonSize ? socketOptions.maxBsonSize : Connection.DEFAULT_MAX_BSON_SIZE;
27+
this.maxMessageSizeBytes = socketOptions.maxMessageSizeBytes ? socketOptions.maxMessageSizeBytes : Connection.DEFAULT_MAX_MESSAGE_SIZE;
2728
// Contains the current message bytes
2829
this.buffer = null;
2930
// Contains the current message size
@@ -42,6 +43,8 @@ var Connection = exports.Connection = function(id, socketOptions) {
4243

4344
// Set max bson size
4445
Connection.DEFAULT_MAX_BSON_SIZE = 1024 * 1024 * 4;
46+
// Set default to max bson to avoid overflow or bad guesses
47+
Connection.DEFAULT_MAX_MESSAGE_SIZE = Connection.DEFAULT_MAX_BSON_SIZE;
4548

4649
// Inherit event emitter so we can emit stuff wohoo
4750
inherits(Connection, EventEmitter);
@@ -183,17 +186,28 @@ Connection.prototype.write = function(command, callback) {
183186
if(Array.isArray(command)) {
184187
for(var i = 0; i < command.length; i++) {
185188
var binaryCommand = command[i].toBinary()
189+
186190
if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
187-
return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
191+
return callback(new Error("Document exceeds maximum allowed bson size of " + this.maxBsonSize + " bytes"));
192+
193+
if(this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxMessageSizeBytes) {
194+
return callback(new Error("Command exceeds maximum message size of " + this.maxMessageSizeBytes + " bytes"));
195+
}
196+
188197
if(this.logger != null && this.logger.doDebug)
189198
this.logger.debug("writing command to mongodb", {binary: binaryCommand, json: command[i]});
190199

191200
var r = this.writeSteam.write(binaryCommand);
192201
}
193202
} else {
194203
var binaryCommand = command.toBinary()
204+
195205
if(!this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxBsonSize)
196-
return callback(new Error("Document exceeds maximal allowed bson size of " + this.maxBsonSize + " bytes"));
206+
return callback(new Error("Document exceeds maximum allowed bson size of " + this.maxBsonSize + " bytes"));
207+
208+
if(this.socketOptions['disableDriverBSONSizeCheck'] && binaryCommand.length > this.maxMessageSizeBytes) {
209+
return callback(new Error("Command exceeds maximum message size of " + this.maxMessageSizeBytes + " bytes"));
210+
}
197211

198212
if(this.logger != null && this.logger.doDebug)
199213
this.logger.debug("writing command to mongodb", {binary: binaryCommand, json: command});

lib/mongodb/connection/connection_pool.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ ConnectionPool.prototype.setMaxBsonSize = function(maxBsonSize) {
8181
}
8282
}
8383

84+
ConnectionPool.prototype.setMaxMessageSizeBytes = function(maxMessageSizeBytes) {
85+
if(maxMessageSizeBytes == null){
86+
maxMessageSizeBytes = Connection.DEFAULT_MAX_MESSAGE_SIZE;
87+
}
88+
89+
for(var i = 0; i < this.openConnections.length; i++) {
90+
this.openConnections[i].maxMessageSizeBytes = maxMessageSizeBytes;
91+
}
92+
}
93+
8494
// Start a function
8595
var _connect = function(_self) {
8696
// return new function() {

lib/mongodb/connection/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ Server.prototype.connect = function(dbInstance, options, callback) {
322322
if(err != null) return internalCallback(err, null, server);
323323
server.master = reply.documents[0].ismaster == 1 ? true : false;
324324
server.connectionPool.setMaxBsonSize(reply.documents[0].maxBsonObjectSize);
325+
server.connectionPool.setMaxMessageSizeBytes(reply.documents[0].maxMessageSizeBytes);
325326
// Set server as connected
326327
server.connected = true;
327328
// Save document returned so we can query it

test/tests/functional/db_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ exports.shouldCorrectlyAccessACollection = function(configuration, test) {
527527
var col1 = db.collection('test_correctly_access_collections');
528528

529529
// Grab a collection with a callback but no safe operation
530-
var db.collection('test_correctly_access_collections', function(err, col2) {
530+
db.collection('test_correctly_access_collections', function(err, col2) {
531531
test.equal(null, err);
532532

533533
// Grab a collection with a callback in safe mode, ensuring it exists (should fail as it's not created)

test/tests/functional/insert_tests.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,27 @@ exports.shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed = function(co
10861086
});
10871087
}
10881088

1089+
/**
1090+
* @ignore
1091+
*/
1092+
exports.shouldFailDueToMessageBeingBiggerThanMaxMessageSize = function(configuration, test) {
1093+
var Binary = configuration.getMongoPackage().Binary;
1094+
1095+
var db = configuration.newDbInstance({w:1}, {disableDriverBSONSizeCheck:true})
1096+
db.open(function(err, db) {
1097+
var binary = new Binary(new Buffer(db.serverConfig.checkoutWriter().maxBsonSize));
1098+
var collection = db.collection('shouldFailDueToInsertBeingBiggerThanMaxDocumentSizeAllowed');
1099+
1100+
collection.insert([{doc:binary}, {doc:binary}, {doc:binary}, {doc:binary}], {w:1}, function(err, result) {
1101+
test.ok(err != null);
1102+
test.ok(err.message.match('Command exceeds maximum'))
1103+
1104+
db.close();
1105+
test.done();
1106+
});
1107+
})
1108+
}
1109+
10891110
/**
10901111
* @ignore
10911112
*/

0 commit comments

Comments
 (0)