Is global variable assignment atomic on NodeJS?

扶醉桌前 提交于 2021-01-29 11:19:16

问题


I’m working on a stateful and websocket server on Node. We require a piece of read-only data that is important for the biz logic and save it in Node’s global scope, and recently we have to invalidate this require cache and re-require to allow changes of this piece of data at runtime. My question is should we worry simply reassigning the global variable the result of the re-require would cause issues between concurrent connections which read this single copy of data at different phases? Thanks!


回答1:


Is global variable assignment atomic on NodeJS?

Because node.js runs your Javascript as single threaded only, all assignments are atomic in Javascript.

Should we worry simply reassigning the global variable the result of the re-require would cause issues between concurrent connections which read this single copy of data at different phases?

You don't have to worry about an assignment not being atomic.

If a line of code reads this global variable and assigns its value to another local variable before the reassignment to the global var, would the value of this local variable be automatically refreshed after the reassignment?

Whether the module that previously had access to the data will see the new data after assignment or not depends upon the precise details of the code and what's in the variables. To comment specifically on your situation, you'd have to show us the actual code and what is in the variables that are assigned in the operation.

Here's a general description of assignment in Javascript. Assignment does something different based on what's in the variable.

Primitives are copied when assigned. Objects are assigned by just sharing a pointer to the object (no copying). So anyone with a pointer to the same object will see all property changes on that object since they point at the exact same object. But, if you assign a new object to the master variable, then all others will still have a pointer to the previous object not the new object.

Here are a few examples to illustrate:

let master = {name: "John"};

// x gets pointer to the same object that master points to
// both master and x point to the same object
let x = master;
console.log(x.name);    // "John"

// update property on master
// but, both variables point to the same object so both see
// the change in the property value on that object
master.name = "Alice";
console.log(master.name);  // "Alice"
console.log(x.name);       // "Alice"
console.log(x === master); // true, they both point to the same object

// reassign a new object to master variable
master = {name: "Drew"};

// x still points at original object that was originally in master
// and knows nothing about the whole new object in master now
console.log(master.name);  // "Drew" 
console.log(x.name);       // still "Alice", not "Drew"
console.log(x === master); // false, not the same object

Primitive data types such as numbers or booleans are assigned by copy so after assigning the contents of one variable to another, there is no connection at all between the two variables. They each have their own content of the data so changing the value of one variable has no effect on the content of the other variable.

 // primitive data value
 let master = 2;

 let x = master;
 console.log(x);     // 2

 master = 10;
 console.log(x);     // still 2


来源:https://stackoverflow.com/questions/53128654/is-global-variable-assignment-atomic-on-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!