Giter Site home page Giter Site logo

memcache's Introduction

Build Status

memcache

NodeJS memcached client with the most efficient ASCII protocol parser.

Features

  • Very efficient memcached ASCII protocol parser by using only NodeJS Buffer APIs.
  • Optional compression for the data before sending to memcached
  • Auto reconnects when there's network error or timeout
  • Support sending arbitrary commands. Read up on the protocol doc here.
  • Support storing string, numeric, and JSON values
  • APIs Support callback or Promise
  • Support fire and forget requests
  • Support multiple connections
  • Support TLS connections

Packages

This repo uses lerna to manage multiple packages.

Development environment installation

  1. Clone this repo
  2. run nvm use
  3. run npm ci
  4. run npm run bootstrap
  5. optional: just to make sure everything is fine run npm run build and check if there's no error using that command
  6. Testing: run npm test

Publishing notes

  • Recommended publish flow is to publish the modules individually using npm publish, can be improved to use lerna in the future

Other Implementations

License

Apache-2.0 © Joel Chen

memcache's People

Contributors

hyanezs avatar jchip avatar kylejeske avatar nfriedly avatar quchen88 avatar rgrove avatar rubenbarreiro avatar smuthya avatar srinathm85 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

memcache's Issues

Feature request: support for meta commands

Memcached has some new 'meta commands' that extend the text protocol, but do not appear to be supported in memcached-client (and presumably not in the njs server either. I'm not sure if the parser would need changes.)

There is an overview at https://github.com/memcached/memcached/wiki/MetaCommands

They are fully documented here:

I tried using cmd() with a meta command to retrieve the expiration time of a key that I had previously set:

client.cmd('mg mykey t')
  .then(console.log.bind(console, 'success'))
  .catch(console.error.bind(console, 'error'))

but it logs an error and then times out:

No command action defined for [ 'HD', 't31' ]
error Error: Command timeout
[stack trace]

(In the command, mg is for "meta get", t indicates that I want the expiration time. In the response, HD indicates that there is no value, only headers, and t31 indicates that there are 31 seconds left until the expiration.)

I also tried adding the v flag to request a value in addition to the expiration time. It sort-of worked, in that it returned the value rather than timing out. But it still complained rather than returning the expiration time:

No command action defined for [ 'VA', '1', 't60' ]
success 2

I particularly want the Meta Arithmetic command so that I can use it in the rate-limit-memcached rewrite that we're working on to do an "upsert" increment and retrieve the value and TTL all in a single command.

I'd be willing to do some of the work to add these new commands, but I thought it would be smart to start a discussion here to plan out the overall shape of things.

Lifetime does not work.

Setting the lifetime in the connection settings as so:

this.memcacheClient = new memcacheClient({ server: { server: connectStr, maxConnections: 150 }, lifetime: 0 })

or

this.memcacheClient.set(key, digest, { lifetime: 0, noreply: true });

seems to have no effect.

Please add ITEM support

Please add ITEM response support
after invoking command 'stats cachedump'
at lease one line of code
ITEM: ACTION_RESULT,
to line 20 (for example) to memcache-client/lib/cmd-actions.js

will fix a problem

THANKS!

client test failures

I noticed a few memcache-client test cases fail when I run npm test locally. I don't think this is a functional bug as the client works fine. This is most likely a test issue which would be nice to fix.

FAIL  src/test/spec/client.spec.ts (7.634 s) (10 failures)
  memcache client
    ✕ should not get system ETIMEDOUT error after custom connect timeout (2 ms)
    ✕ should set and get multiple keys concurrently with 1 maxConnections (14 ms)
    ✕ should set and get multiple keys concurrently with 2 maxConnections (11 ms)
    ✕ should set and get multiple keys concurrently with 3 maxConnections (7 ms)
    ✕ should set and get multiple keys concurrently with 4 maxConnections (7 ms)
    ✕ should set and get multiple keys concurrently with 5 maxConnections (5 ms)
    ✕ should set and get multiple keys concurrently with 10 maxConnections (3 ms)
    ✕ should handle error if can't JSON.stringify value (6 ms)
    ✕ should gracefully propagate decompress error (5002 ms)
    ✕ should handle command timeout error (105 ms)

performance-regression, which decreases throughput 5-10 times

socket.once("connect", () => {
this.socket = socket;

Can you please add socket.setNoDelay(true) before line 111 ?
This will increases the throughput greatly (5-10 times in my personal-benchmark).

After that, Nagle is deactivated. This will trigger a lot of small packets to be written on the network for modify-commands.
So can you please aggregate the multiple socket.write() calls to single ones on:

socket.write(data);
if (options && options.noreply) {
socket.write(" noreply\r\n");
} else {
socket.write("\r\n");
}

and on:
const msg = `${cmd} ${key} ${packed.flag} ${lifetime} ${bytes}${casUniq}${noreply}\r\n`;
socket.write(msg);
socket.write(packed.data);
socket.write("\r\n");

Please add TLS support

Memcached started support TLS since 1.5.13. It would be nice to give people this option if they want/need to use it.

TypeError: setting 'error' on unknown `CLIENT_ERROR bad command line format`

Hey, been using memcache-client since September in our open-source project, mostly everything has been great so thank you! I've encountered an unusual error that has occurred ~50 times on our servers over the last month, which crashes it due to an uncaught exception.

The stack trace goes like this:

TypeError: Cannot set properties of undefined (setting 'error')
/server/node_modules/memcache-client/dist/lib/connection.js in MemcacheConnection.receiveResult at line 188:32

                retrieve.results[pending.cmdTokens[1]] = {
                    tokens: pending.cmdTokens,
                    casUniq: pending.cmdTokens[4],
                    value: (_a = this.client) === null || _a === void 0 ? void 0 : _a._unpackValue(pending),
                };
            }
            catch (err) {
>>>Exception>>>>           retrieve.error = err;
            }
        }
        delete pending.data;
    }
    shutdown() {
        this._shutdown("Shutdown requested");
    }
/server/node_modules/memcache-parser/dist/lib/memcache-parser.js in MemcacheConnection._copyPending at line 157:26
/server/node_modules/memcache-parser/dist/lib/memcache-parser.js in MemcacheConnection._parseCmd at line 101:29
/server/node_modules/memcache-parser/dist/lib/memcache-parser.js in MemcacheConnection._processData at line 197:25
/server/node_modules/memcache-parser/dist/lib/memcache-parser.js in MemcacheConnection.onData at line 28:25
node:events in Socket.emit at line 514:28
node:domain in Socket.emit at line 488:12
node:internal/streams/readable in addChunk at line 376:12
node:internal/streams/readable in readableAddChunk at line 349:9
node:internal/streams/readable in Readable.push at line 286:10
node:internal/stream_base_commons in TCP.onStreamRead at line 190:23
CLIENT_ERROR bad command line format

It appears that retrieve can be null here: https://github.com/electrode-io/memcache/blob/master/packages/memcache-client/src/lib/connection.ts#L235, when corresponding with CLIENT_ERROR bad command line format. I don't know the specific command that causes this CLIENT_ERROR, but regardless, the memcache client crashes as a whole. Initially I increased maxConnections, as I thought it was some networking error.

Any ideas? Thanks!

Confirmed this error persists on 1.0.5.

Memcache-Client Accepts Non-Compliant Keys

The memcache-client will accept keys that would cause the send to fail, you wont see this with the value as the data is packed. According to the ASCII protocol keys should be encoded, see: https://github.com/memcached/memcached/blob/ca66b826f25e1db83d191780e0bcac4a070c6911/doc/protocol.txt#L595-L598

The value of keys (and potentially other things) are "URI encoded". Since most
keys used conform to standard ASCII, this should have no effect. For keys with
less standard or binary characters, "%NN"'s are inserted to represent the
byte, ie: "n%2Cfoo" for "n,foo".

One thing to note is that when a bad key is sent across the wire it puts the client in a bad state. The dequeued command fails, i'm not sure why. Subsequent commands will also fail.

I'm not sure key encoding should be an application concern, however, it seems like an oversight given you're able to easily break the existing delegate methods like set and get and put the client in a non-working state. Let me know if you need more info.


Reproduction

// Won't Work
someClient.set("my bad key", "my goo value")

// Work
someClient.set(encodeURIComponent(badKeyVal), value, (err, res) => console.log(res));

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.