SlideShare ist ein Scribd-Unternehmen logo
1 von 12
Downloaden Sie, um offline zu lesen
continuation-local storage
https://github.com/othiym23/continuationlocal-storage
cls
needed a way to pass around per-request state that wouldn’t break (or
change) user code

stashing state on the side of req and res is yucky

thread-local storage is nice but Node is single-threaded

and we’re thinking about chains of continuations, so each set of values
needs to be tied to a specific request chain
a dirt-simple example
var	
  cls	
  =	
  require('continuation-­‐local-­‐storage');	
  
	
  	
  
function	
  magic()	
  {	
  
	
  	
  console.log("value	
  is	
  %s",	
  cls.getNamespace('test').get('value'));	
  
}	
  
	
  	
  
var	
  test	
  =	
  cls.createNamespace('test');	
  
test.run(function	
  ()	
  {	
  
	
  	
  test.set('value',	
  'hi	
  there!');	
  
	
  	
  setImmediate(magic);	
  
});	
  
test.set('value',	
  'oh	
  no!');	
  
things to note
magic does not have test in scope

inside test.run, values have their own scope

test’s values persist across the call to setImmediate
what’s going on?
namespace.run clones the current context

the contexts are entered and exited like domains

the state is persisted across process.nextTick, timers, and all other core
async functions
{create,add,remove}AsyncListener
https://github.com/joyent/node/pull/6011
https://github.com/othiym23/async-listener
THERE’S NO WAY I’M LETTING ANOTHER THING AS SLOW AS DOMAINS IN CORE
– TREVNORRIS
srs bzns
very powerful but low-level API

captures every async event in the Node process and makes it observable

makes it simple to pass state to callbacks without changing their code

also makes it easy to write global error handlers that have some state
the API
a listener function that is fired for every async event, returns a value that
will be passed to the decorators on this async event’s callbacks

a bundle of optional before, after, and error callbacks that will be wrapped
around functions that this async event is responsible for

an optional value that can be passed to before / after / error instead of the
results of the listener
how it go??!
in 0.11.8 and earlier, a JavaScript polyfill that monkeypatches all the things
(so, pretty slow)

in 0.11.9+, a whole bunch of code in C++ and JavaScript (but still, not a
performance king)
how it stable?
polyfill has pretty good coverage, doesn’t appear to slow apps down too
much in practice (just recently solidified enough)

polyfill is in use in the New Relic transaction tracer

the native version is stable enough that Trevor rewrote domains to use it
(and got a nice performance boost when domains aren’t in use)
what for?
CLS, obviously

low-level logging modules

pure JS profilers

long stacktrace modules

making the details of asynchronous execution more observable, basically

Weitere ähnliche Inhalte

Was ist angesagt?

Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 

Was ist angesagt? (20)

Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016The Road To Reactive with RxJava JEEConf 2016
The Road To Reactive with RxJava JEEConf 2016
 
Introduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoaIntroduction to reactive programming & ReactiveCocoa
Introduction to reactive programming & ReactiveCocoa
 
AWS Java SDK @ scale
AWS Java SDK @ scaleAWS Java SDK @ scale
AWS Java SDK @ scale
 
Forgive me for i have allocated
Forgive me for i have allocatedForgive me for i have allocated
Forgive me for i have allocated
 
Functional Reactive Programming in Clojurescript
Functional Reactive Programming in ClojurescriptFunctional Reactive Programming in Clojurescript
Functional Reactive Programming in Clojurescript
 
Behind modern concurrency primitives
Behind modern concurrency primitivesBehind modern concurrency primitives
Behind modern concurrency primitives
 
RxJava Applied
RxJava AppliedRxJava Applied
RxJava Applied
 
Non Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJavaNon Blocking I/O for Everyone with RxJava
Non Blocking I/O for Everyone with RxJava
 
Real world functional reactive programming
Real world functional reactive programmingReal world functional reactive programming
Real world functional reactive programming
 
The evolution of asynchronous javascript
The evolution of asynchronous javascriptThe evolution of asynchronous javascript
The evolution of asynchronous javascript
 
Practical RxJava for Android
Practical RxJava for AndroidPractical RxJava for Android
Practical RxJava for Android
 
Practical JavaScript Promises
Practical JavaScript PromisesPractical JavaScript Promises
Practical JavaScript Promises
 
Akka.NET streams and reactive streams
Akka.NET streams and reactive streamsAkka.NET streams and reactive streams
Akka.NET streams and reactive streams
 
JavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good PartsJavaOne 2013: Java 8 - The Good Parts
JavaOne 2013: Java 8 - The Good Parts
 
Code generation with javac plugin
Code generation with javac pluginCode generation with javac plugin
Code generation with javac plugin
 
Avoiding callback hell with promises
Avoiding callback hell with promisesAvoiding callback hell with promises
Avoiding callback hell with promises
 
Akka Futures and Akka Remoting
Akka Futures  and Akka RemotingAkka Futures  and Akka Remoting
Akka Futures and Akka Remoting
 
Promise pattern
Promise patternPromise pattern
Promise pattern
 
DDDing Tools = Akka Persistence
DDDing Tools = Akka PersistenceDDDing Tools = Akka Persistence
DDDing Tools = Akka Persistence
 

Ähnlich wie CLS & asyncListener: asynchronous observability for Node.js

NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
rantav
 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
Dinh Pham
 

Ähnlich wie CLS & asyncListener: asynchronous observability for Node.js (20)

Server side JavaScript: going all the way
Server side JavaScript: going all the wayServer side JavaScript: going all the way
Server side JavaScript: going all the way
 
Clojure concurrency
Clojure concurrencyClojure concurrency
Clojure concurrency
 
Functional Java 8 - Introduction
Functional Java 8 - IntroductionFunctional Java 8 - Introduction
Functional Java 8 - Introduction
 
JS everywhere 2011
JS everywhere 2011JS everywhere 2011
JS everywhere 2011
 
OMG Namespaces! | Raffaele Di Fazio
OMG Namespaces! | Raffaele Di FazioOMG Namespaces! | Raffaele Di Fazio
OMG Namespaces! | Raffaele Di Fazio
 
NOSQL and Cassandra
NOSQL and CassandraNOSQL and Cassandra
NOSQL and Cassandra
 
Devoxx
DevoxxDevoxx
Devoxx
 
NodeJS
NodeJSNodeJS
NodeJS
 
Building an aws sdk for Perl - Granada Perl Workshop 2014
Building an aws sdk for Perl - Granada Perl Workshop 2014Building an aws sdk for Perl - Granada Perl Workshop 2014
Building an aws sdk for Perl - Granada Perl Workshop 2014
 
State management in a GraphQL era
State management in a GraphQL eraState management in a GraphQL era
State management in a GraphQL era
 
Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?Asynchronous I/O in NodeJS - new standard or challenges?
Asynchronous I/O in NodeJS - new standard or challenges?
 
Liftweb
LiftwebLiftweb
Liftweb
 
Cocoa heads 09112017
Cocoa heads 09112017Cocoa heads 09112017
Cocoa heads 09112017
 
Kotlin Coroutines and Rx
Kotlin Coroutines and RxKotlin Coroutines and Rx
Kotlin Coroutines and Rx
 
JavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptxJavaScript Multithread or Single Thread.pptx
JavaScript Multithread or Single Thread.pptx
 
[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기
 
Search for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code AnalysisSearch for Vulnerabilities Using Static Code Analysis
Search for Vulnerabilities Using Static Code Analysis
 
Re usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertionsRe usable continuous-time analog sva assertions
Re usable continuous-time analog sva assertions
 
Panama.pdf
Panama.pdfPanama.pdf
Panama.pdf
 
Cassandra Day SV 2014: Netflix’s Astyanax Java Client Driver for Apache Cassa...
Cassandra Day SV 2014: Netflix’s Astyanax Java Client Driver for Apache Cassa...Cassandra Day SV 2014: Netflix’s Astyanax Java Client Driver for Apache Cassa...
Cassandra Day SV 2014: Netflix’s Astyanax Java Client Driver for Apache Cassa...
 

Kürzlich hochgeladen

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

CLS & asyncListener: asynchronous observability for Node.js

  • 2. cls needed a way to pass around per-request state that wouldn’t break (or change) user code stashing state on the side of req and res is yucky thread-local storage is nice but Node is single-threaded and we’re thinking about chains of continuations, so each set of values needs to be tied to a specific request chain
  • 3. a dirt-simple example var  cls  =  require('continuation-­‐local-­‐storage');       function  magic()  {      console.log("value  is  %s",  cls.getNamespace('test').get('value'));   }       var  test  =  cls.createNamespace('test');   test.run(function  ()  {      test.set('value',  'hi  there!');      setImmediate(magic);   });   test.set('value',  'oh  no!');  
  • 4. things to note magic does not have test in scope inside test.run, values have their own scope test’s values persist across the call to setImmediate
  • 5. what’s going on? namespace.run clones the current context the contexts are entered and exited like domains the state is persisted across process.nextTick, timers, and all other core async functions
  • 7. THERE’S NO WAY I’M LETTING ANOTHER THING AS SLOW AS DOMAINS IN CORE – TREVNORRIS
  • 8. srs bzns very powerful but low-level API captures every async event in the Node process and makes it observable makes it simple to pass state to callbacks without changing their code also makes it easy to write global error handlers that have some state
  • 9. the API a listener function that is fired for every async event, returns a value that will be passed to the decorators on this async event’s callbacks a bundle of optional before, after, and error callbacks that will be wrapped around functions that this async event is responsible for an optional value that can be passed to before / after / error instead of the results of the listener
  • 10. how it go??! in 0.11.8 and earlier, a JavaScript polyfill that monkeypatches all the things (so, pretty slow) in 0.11.9+, a whole bunch of code in C++ and JavaScript (but still, not a performance king)
  • 11. how it stable? polyfill has pretty good coverage, doesn’t appear to slow apps down too much in practice (just recently solidified enough) polyfill is in use in the New Relic transaction tracer the native version is stable enough that Trevor rewrote domains to use it (and got a nice performance boost when domains aren’t in use)
  • 12. what for? CLS, obviously low-level logging modules pure JS profilers long stacktrace modules making the details of asynchronous execution more observable, basically