Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a SERVER_ERROR when value length is just under the max value length #272

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/memcached.js
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ Client.config = {
value = Utils.escapeValue(value);

length = Buffer.byteLength(value);
if (length > this.maxValue) {
if (length + fullkey.length + 71 > this.maxValue) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment explaining the logic behind this (and the magic constant that is 71) would be appreciated :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation I found said that 71 is the length of the metadata that memcache uses to store information about each key value pair. That may need to be adjusted or set as a system variable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to keep in mind is that memcache clients can also talk to Couchbase servers. I don't know what the limitations are there? In any case, I think a code comment or well chosen constant name would be nice.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the tips. We use it with both memcached and couchbase ourselves. I will do a bit more research and tweak the code accordingly.
Thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

return privates.errorResponse(new Error('The length of the value is greater than ' + this.maxValue), callback);
}

Expand Down
24 changes: 24 additions & 0 deletions test/memcached-get-set.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,28 @@ describe("Memcached GET SET", function() {
done();
});
});

/**
* Set value size just under maximum amount of data (1MB),
* should trigger error, not crash. The actual value size to trigger the
* error should be value length + key length + length of metadata stored with the
* key value pair.
*/
it("set value length just under maximum data and check for correct error handling", function(done) {
var memcached = new Memcached(common.servers.single)
, message = new Array(32767).join('ThisIsMyTestMessageForThisData32')
, testnr = ++global.testnumbers
, callbacks = 0;
//Length of message is 1048512
memcached.set("test:" + testnr, message, 1000, function(error, ok){
++callbacks;

assert.equal(error, 'Error: The length of the value is greater than 1048576');
ok.should.be.false;

memcached.end(); // close connections
assert.equal(callbacks, 1);
done();
});
});
});