Git Product home page Git Product logo

node-stack-cls's Introduction

Node.js Continuation Local Storage (CLS) based on call-stack

Continuation Local Storage(CLS) works like thread-local storage in threaded programming,
but is based on chains of Node-style callbacks instead of threads.
CLS is very useful to share contexts across async calls in Node.js.

Most CLS modules are implemented based on async_hooks. It is a good feature for CLS.
But it is still experimental feature and seems to have performance issues.
So we made a simple CLS module using call-stack, not async_hooks.
The main concept is to mark the unique context key on the call-stack
and using this key, share the same context across the functions on call-stack.

Attention:
This module is implemented on call-stack basis. so, context cannot be shared in callback function.
Because, callback function is performed with a separated call-stack.
To expand the context to callback function, you need to wrap the callback function once more.
Therefore, this module is recommended for async/await based processing.

Installation

This is a Node.js module available through the npm registry.
Before installing, download and install Node.js. node.js v7.6.0 or higher for ES2015 and async function support.

$ npm install node-stack-cls

Example

By wrapping your event handler function, the wrapped subcalls can share the same context storage.
Under wrapped function, can set/get context anywhere.

import nscls from 'node-stack-cls';

async function handler(event) {
  // set context
  nscls.setContext('foo', 1);
  nscls.setContext('bar', 2);

  await process1();
  await process2();
  return 'ok'
}

async function process1() {
  // get context
  console.log(nscls.getContext('foo'));
  // console out: 1
}

async function process2() {
  // get context
  console.log(nscls.getContext('bar'));
  // console out: 2
}

// wrap user handler function
export default nscls.wrapper(handler);

Expand context to callback function

To expand context to callback function, wrap your callback function once more.

async function handler(event) {
  nscls.setContext('foo', 1);

  asyncProcess().then(nscls.wrapper(result => {
    nscls.getContext('foo');
    // do asynchronous process
  }));

  setTimeout(nscls.wrapper(() => {
    nscls.getContext('foo');
    // do asynchronous process
  }), 10000);

  return 'ok';
}

Note:
The context storage is allocated at the time the wrapper is created (not invoke time)
and released at the time the wrapper is completed.
If the wrapper is not invoked within a certain time, release the context storage.
This is to prevent memory leaks, you can change this timer with config api. (default: 60sec)

Apply to middleware

If use Koa, you can apply wrapper to middleware.

import Koa from 'koa';
import nscls from 'node-stack-cls';

const app = new Koa();

app.use(async (ctx, next) => {
  await nscls.wrapper(next)();
});

If use express, you can also apply wrapper to middleware.

import express from 'express';
import nscls from 'node-stack-cls';

const app = new express();

app.use(async (req, res, next) => {
  await nscls.wrapper(next)();
});

API

Wrap top function for context sharing

wrapper(func)

  • func: target function to wrap
  • return: wrapper function
await nscls.wrapper(async () => {
  // do something
})();

// can invoke wrapper with wrapped function args
await nscls.wrapper(async (arg1, arg2) => {
  // do something
})(arg1, arg2);

Access context

setContext(key, value)

  • key: context key to set
  • value: context value to set
  • return: none

getContext(key)

  • key: context key to get
  • return: context value (if not found, return undefined)
nscls.setContext('foo', 1);

nscls.getContext('foo'); // return 1
nscls.getContext('bar'); // return undefined

Change config

config(option)

  • option: option object
    • wrapperPrefix: wrapper function name prefix (default: nscls-wrapper)
    • wrapperWaitTime: wait msec for wrapper invocation (default: 60000)
  • return: none
// if you want to change config, please set before start to handle events.
nscls.config({
  wrapperPrefix: 'myprefix', // default: 'nscls-wrapper'
  wrapperWaitTime: 120000    // default: 60000
});

node-stack-cls's People

Contributors

sungtaek avatar

Stargazers

 avatar

Watchers

 avatar

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.