mercredi 30 novembre 2016

require-uncached thread safety, creating new instances in javascript

I have a system that could be serving a high throughput of HTTP requests, and I am trying to load a custom module with a new instance for each request. The reason I want to create a new instance is because within the module is an object that is shared across promises in multiple promise chains. I would like the object to be isolated for each instance of this module. I realize this may be considered an anti-pattern in some shops, but I like having a single dynamic object that can be used across many complex promise chains, as it allows my promises to be reusable in many different wans. I like how the promise chains organize my code, and the pattern itself fits my needs well, but would like to hear about alternatives if any. Here is an example:

var reusableObject;

module.exports = function() {
    return {
        myFunction: function() {
            return somePromise()
            .then(anotherPromise)
            .then(yetAnotherPromise)
            .then(function() { return reusableObject; })
        }
}

I was unsuccessful in creating new instances with the new operator with the statement var service = new (require('myModule'))(). My best guess was that underneath, requirejs is fetching the module from cache.

I came across a module called require-uncached, and voila, I was successfully able to create new instances of my module. However, when glancing at the code, I can't tell if this module is going to be thread-safe. I realized it's time to get some expert analysis. Any help is appreicated. tia

Aucun commentaire:

Enregistrer un commentaire