skonves / express-http-context

Get and set request-scoped context anywhere
MIT License
299 stars 26 forks source link

why not using node internal AsyncLocalStorage? #52

Open tofixx opened 3 years ago

tofixx commented 3 years ago

The current node lts supports using internal async_hooks which does not break in some reasons, cls-hooks fails.

The middleware could use https://nodejs.org/docs/latest-v14.x/api/async_hooks.html#async_hooks_asynclocalstorage_run_store_callback_args instead.

PhakornKiong commented 3 years ago

@tofixx May i know how cls-hook is failing? Technically it is based on the same async-hooks (for nodejs ver 8+) and it should works (There are some small bugs in ns.runPromise and how memory usage is not optimized), although the API is not consistent with that of ALS.

I'm in favor for using ALS as it is optimized in V8 layer. Some microbenchmarking results here https://github.com/Darkripper214/ALS-CLS-Benchmark

@skonves I've came up with a consistent async-context package that supports v 8.12 onwards and will use Nodejs' AsyncLocalStorage when needed. It also supports the legacy methods that cls-hooked have as well. Are you open to migration? https://github.com/Darkripper214/ALS-Context

dhalbrook commented 2 years ago

The asynchronous-local-storage package has a very similar API to this one and uses AsyncLocalStorage if that helps.