All Projects → vicanso → async-local-storage

vicanso / async-local-storage

Licence: other
local storage that is shared between all stack frames (top down) in a call chain, including async function

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to async-local-storage

nestjs-cls
A continuation-local storage (async context) module compatible with NestJS's dependency injection.
Stars: ✭ 110 (+50.68%)
Mutual labels:  async-hooks
Plmcodetemplate
给部门制定的代码框架模板
Stars: ✭ 1,871 (+2463.01%)
Mutual labels:  threadlocal
Transmittable Thread Local
📌 TransmittableThreadLocal (TTL), the missing Java™ std lib(simple & 0-dependency) for framework/middleware, provide an enhanced InheritableThreadLocal that transmits values between threads even using thread pooling components.
Stars: ✭ 4,678 (+6308.22%)
Mutual labels:  threadlocal
context-propagation
Propagate snapshots of ThreadLocal values to another thread
Stars: ✭ 15 (-79.45%)
Mutual labels:  threadlocal

async-local-storage

!!!Please use AsyncLocalStorage instead of async-local-storage.

Build Status Coverage Status npm Github Releases

I want something like thread-local storage in threaded programming and async_hooks is usable in node.js 8.0, so there is an easy way to use thread-local.

API

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  als.scope();
  const id = randomBytes(8);
  als.set('id', id);
  delay().then(() => {
    assert.equal(als.get('id'), id);
    return readfilePromise(__filename);
  }).then(() => {
    assert.equal(als.get('id'), id);
    return superagent.get('http://www.baidu.com/');
  }).then(() => {
    assert.equal(als.get('id'), id);
  });
}, 100);

enable

enable the async hooks

const als = require('async-local-storage');
als.enable();

disable

disable the async hooks

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  als.disable();
}, 100);

size

get the size of storage

const als = require('async-local-storage');
als.enable();
setTimeout(() => {
  console.info(als.size());
}, 100);

scope

change the scope of call chain, it will be the call chain top (remove the parent of itself)

const als = require('async-local-storage');
const Koa = require('koa');
const assert = require('assert');

const app = new Koa();
app.use(async (ctx, next) => {
  const id = ctx.get('X-Request-Id');
  als.scope();
  als.set('id', id);
  await next();
});

app.use(async (ctx, next) => {
  const id = ctx.get('X-Request-Id');
  assert.equal(als.get('id'), id);
  await next();
});

app.use((ctx) => {
  ctx.body = 'OK';
});

set

set the value by key for the current id

  • key the key
  • value the value
  • linkedTop set the value linked to top
als.enable()
setTimeout(() => {
  als.scope();
  const id = randomBytes();
  setTimeout(() => {
    als.set('id', id, true);
  }, 1);
  setTimeout(() => {
    assert.equal(als.get('id'), id);
  }, 10);
}, 10);

get

get the value by key, if will find from parent, self --> parent --> parent, until the value is not undefined

  • key the key
als.enable();
setTimeout(() => {
  als.scope();
  const id = randomBytes();
  setTimeout(() => {
    als.set('id', id, true);
  }, 1);
  setTimeout(() => {
    assert.equal(als.get('id'), id);
  }, 10);
}, 10);

enableLinkedTop

enable linked top for default (default is disabled)

als.enable();
als.enableLinkedTop();
setTimeout(() => {
  als.scope();
  setTimeout(() => {
    // the same as als.set('id', 'a', true)
    als.set('id', 'a');
  }, 10);
}, 10);

disableLinkedTop

disable linked top for default

als.enable();
als.enableLinkedTop();
setTimeout(() => {
  als.disableLinkedTop();
  als.scope();
  setTimeout(() => {
    // the same as als.set('id', 'a', false)
    als.set('id', 'a');
  }, 10);
}, 10);

currentId

get the current id

const assert = require('assert');
als.enable();
setTimeout(() => {
  console.info(als.currentId());
}, 10);

use

get the use time of id

  • id The tigger id, default is als.currentId()
als.enable()
setTimeout(() => {
  const id = als.currentId();
  console.info(als.use(id));
}, 10);

enableCreateTime

enable create time of data, default is enabled.

als.enableCreateTime();

disableCreateTime

disable create time of data, it can save memory.

als.disableCreateTime();
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].