robertbira

Italian Translation Report: Node.js [Part 48 - 1306 words]

Nov 3rd, 2018
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. For Node.js programs that scale to multiple machines, the terminating proxy or service registry can take note of the failure, and react accordingly.
  2. For example, this is not a good idea:
  3. XXX WARNING! BAD IDEA!
  4. The error won't crash the process, but what it does is worse!
  5. Though we've prevented abrupt process restarting, we are leaking resources like crazy if this ever happens.
  6. This is no better than
  7. By using the context of a domain, and the resilience of separating our program into multiple worker processes, we can react more appropriately, and handle errors with much greater safety.
  8. Much better!
  9. A more realistic scenario would have more than 2 workers, and perhaps not put the master and worker in the same file.
  10. It is also possible to get a bit fancier about logging, and implement whatever custom logic is needed to prevent DoS attacks and other bad behavior.
  11. See the options in the cluster documentation.
  12. The important thing is that the master does very little, increasing our resilience to unexpected errors.
  13. This is where we put our bugs!
  14. See the cluster documentation for more details about using worker processes to serve requests.
  15. How it works, caveats, etc.
  16. Note: We're in dangerous territory!
  17. By definition, something unexpected occurred, which we probably didn't want.
  18. Anything can happen now! Be very careful!
  19. make sure we close down within 30 seconds
  20. But don't keep the process open just for that!
  21. stop taking new requests.
  22. Let the master know we're dead.
  23. This will trigger a in the cluster master, and then it will fork a new worker.
  24. try to send an error to the request that triggered the problem
  25. oh well, not much we can do at this point.
  26. Because req and res were created before this domain existed, we need to explicitly add them.
  27. See the explanation of implicit vs explicit binding below.
  28. Now run the handler function in the domain.
  29. This part is not important.
  30. Just an example routing thing.
  31. Put fancy application logic here.
  32. We do some async stuff, and then...
  33. Additions to Error objects
  34. Any time an object is routed through a domain, a few extra fields are added to it.
  35. The domain that first handled the error.
  36. The event emitter that emitted an event with the error object.
  37. The callback function which was bound to the domain, and passed an error as its first argument.
  38. A boolean indicating whether the error was thrown, emitted, or passed to a bound callback function.
  39. Implicit Binding
  40. If domains are in use, then all new objects (including Stream objects, requests, responses, etc.) will be implicitly bound to the active domain at the time of their creation.
  41. Additionally, callbacks passed to lowlevel event loop requests (such as to, or other callback-taking methods) will automatically be bound to the active domain
  42. If they throw, then the domain will catch the error.
  43. In order to prevent excessive memory usage, objects themselves are not implicitly added as children of the active domain.
  44. If they were, then it would be too easy to prevent request and response objects from being properly garbage collected.
  45. To nest objects as children of a parent they must be explicitly added.
  46. Implicit binding routes thrown errors and events to the event, but does not register the on the
  47. Implicit binding only takes care of thrown errors and events.
  48. Explicit Binding
  49. Sometimes, the domain in use is not the one that ought to be used for a specific event emitter. Or, the event emitter could have been created in the context of one domain, but ought to instead be bound to some other domain.
  50. For example, there could be one domain in use for an HTTP server, but perhaps we would like to have a separate domain to use for each request.
  51. That is possible via explicit binding.
  52. create a top-level domain for the server
  53. server is created in the scope of
  54. however, we'd prefer to have a separate domain for each request.
  55. create it first thing, and add and to it.
  56. The class encapsulates the functionality of routing errors and uncaught exceptions to the active object.
  57. is a child class of
  58. To handle the errors that it catches, listen to its event.
  59. An array of timers and event emitters that have been explicitly added to the domain.
  60. emitter or timer to be added to the domain
  61. Explicitly adds an emitter to the domain.
  62. If any event handlers called by the emitter throw an error, or if the emitter emits an event, it will be routed to the domain's event, just like with implicit binding.
  63. This also works with timers that are returned from and
  64. If their callback function throws, it will be caught by the domain handler.
  65. If the Timer or was already bound to a domain, it is removed from that one, and bound to this one instead.
  66. The callback function
  67. The bound function
  68. The returned function will be a wrapper around the supplied callback function. When the returned function is called, any errors that are thrown will be routed to the domain's event.
  69. if this throws, it will also be passed to the domain
  70. an error occurred somewhere.
  71. if we throw it now, it will crash the program with the normal line number and stack message.
  72. The method is plumbing used by the and methods to set the active domain.
  73. It sets and to the domain, and implicitly pushes the domain onto the domain stack managed by the domain module (see for details on the domain stack).
  74. The call to delimits the beginning of a chain of asynchronous calls and I/O operations bound to a domain.
  75. Calling changes only the active domain, and does not alter the domain itself.
  76. and can be called an arbitrary number of times on a single domain.
  77. The method exits the current domain, popping it off the domain stack.
  78. Any time execution is going to switch to the context of a different chain of asynchronous calls, it's important to ensure that the current domain is exited.
  79. The call to delimits either the end of or an interruption to the chain of asynchronous calls and I/O operations bound to a domain.
  80. If there are multiple, nested domains bound to the current execution context, will exit any domains nested within this domain.
  81. The callback function
  82. The intercepted function
  83. This method is almost identical to
  84. However, in addition to catching thrown errors, it will also intercept objects sent as the first argument to the function.
  85. In this way, the common pattern can be replaced with a single error handler in a single place.
  86. note, the first argument is never passed to the callback since it is assumed to be the 'Error' argument and thus intercepted by the domain.
  87. if this throws, it will also be passed to the domain so the error-handling logic can be moved to the 'error' event on the domain instead of being repeated throughout the program.
  88. an error occurred somewhere.
  89. if we throw it now, it will crash the program with the normal line number and stack message.
  90. emitter or timer to be removed from the domain
  91. The opposite of
  92. Removes domain handling from the specified emitter.
  93. Run the supplied function in the context of the domain, implicitly binding all event emitters, timers, and lowlevel requests that are created in that context. Optionally, arguments can be passed to the function.
  94. This is the most basic way to use a domain.
  95. In this example, the handler will be triggered, rather than crashing the program.
  96. Domains and Promises
  97. As of Node.js 8.0.0, the handlers of Promises are run inside the domain in which the call to or itself was made:
  98. A callback may be bound to a specific domain using
  99. Note that domains will not interfere with the error handling mechanisms for Promises, i.e. no event will be emitted for unhandled rejections.
RAW Paste Data