# continuation-local-storage (opens new window)

  • Continuous-Local-Storage는 스레드 프로그래밍에서 thread-local-storage 처럼 작동하지만, 스레드 대신 노드 스타일의 콜백 체인을 기반으로 동작됨. (Continuation-local storage works like thread-local-storage in threaded programming, but is based on chains of Node-style callbacks instead of threads.)
  • Java의 TLS(thread-local-storage)와 같은 기능과 유사하다.
  • Context 객체를 일련의 체인에 연결하여 나중에 접근할 수 있게 하는 기능.
  • CLS는 Node.js >= 8.2.1부터는 node 내장 API인 async_hooks를 사용.

# Sample

var createNamespace = require('continuation-local-storage').createNamespace;
 
var writer = createNamespace('writer');
writer.run(function () {
  writer.set('value', 0);
 
  requestHandler();
});
 
function requestHandler() {
  writer.run(function(outer) {
    // writer.get('value') returns 0
    // outer.value is 0
    writer.set('value', 1);
    // writer.get('value') returns 1
    // outer.value is 1
    process.nextTick(function() {
      // writer.get('value') returns 1
      // outer.value is 1
      writer.run(function(inner) {
        // writer.get('value') returns 1
        // outer.value is 1
        // inner.value is 1
        writer.set('value', 2);
        // writer.get('value') returns 2
        // outer.value is 1
        // inner.value is 2
      });
    });
  });
 
  setTimeout(function() {
    // runs with the default context, because nested contexts have ended
    console.log(writer.get('value')); // prints 0
  }, 1000);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

[!NOTE]

continuation-local-storage (opens new window)가 탄생되고 사용되는 시점에는 Nodejs에서 이런 기능이 없었지만 세상이 변하며, 비동기 리소스 추적을 위한 기능을 만들기 시작한 것으로 보인다.

# Nodejs의 async_hooks

async_hooks (opens new window) 모듈은 비동기 리소스를 추적하는 API를 제공.(Nodejs version >= 8.2.1) async_hooks을 사용해서 만든 cls-hooks (opens new window)도 생겨났다.

이것은 async-listener 대신 asyncWrap OR async_hooks를 사용하는 CLS 버전으로 생각하면 된다.

# async_hooks의 AsyncLocalStorage

Nodejs 자체에서도 CLS역할을 하는 Class를 만들어 제공 하기 시작함. AsyncLocalStorage (opens new window) Added in: v13.10.0, v12.17.0 되어서 이후 버전에서만 사용 할 수 있다.

최신 버전의 Nodejs를 사용 한다면 이제는 AsyncLocalStorage를 사용하자.

# 참고

Last Updated: 11/7/2022, 1:57:59 AM