简体   繁体   English

在nodejs / socket.io伸缩应用程序中序列化资源

[英]Serialize a resource in a nodejs/socket.io scaled application

Currently my nodejs/socket.io server runs in a single thread but there are ways to make it more scalable ( here for example) that I can adopt in the future. 目前,我的nodejs / socket.io服务器在单个线程中运行,但是有一些方法可以使它将来具有更高的可伸缩性(例如, 在这里 )。 But this make all the instances of server running on different processes that are clustered, in that way resources are not serialized anymore and it raises a problem about concurrency. 但是,这使服务器的所有实例都在群集的不同进程上运行,这样资源就不再被序列化了,这引发了并发性问题。 There is a way to serialize certain code segment in different processes on a nodejs/socket.io server basing on a key? 有没有一种方法可以基于密钥在nodejs / socket.io服务器上的不同进程中序列化某些代码段? For example: 例如:

lock(key) 

/*make stuff*/

unlock(key)

This is a example, but it will be appreciate if I can accomplish the same task with promises (I don't know how). 这是一个示例,但是如果我能按承诺完成相同的任务(我不知道怎么做),将不胜感激。

There is no completely generic cross process mutex that magically suspends all other nodejs processes while you are processing something. 在处理某些内容时,没有完全通用的跨进程互斥体可以神奇地挂起所有其他的nodejs进程。

There are, however, many different types of tools that can be used to solve a particular problem. 但是,可以使用许多不同类型的工具来解决特定问题。

The #1 tool is to use appropriate design to minimize or eliminate race conditions between your processes. #1工具是使用适当的设计来最小化或消除过程之间的竞争状况。 How exactly to do this is specific to exactly what problem you are trying to solve so we can provide a lot more helpful info if you describe the specific problem. 具体如何执行完全取决于您要解决的问题,因此,如果您描述特定问题,我们可以提供更多有用的信息。 One common way this is done is by using a common data store and designing your code to use atomic operations within the database. 一种常用的方法是使用公用数据存储并设计代码以在数据库内使用原子操作。 This then uses the database capabilities itself as your control mechanism (something a multi-user database is typically designed to handle very well). 然后,它将数据库功能本身用作您的控制机制(通常设计多用户数据库来处理得很好)。

Beyond that, you can use database locks, file locks or other cross process communications mechanisms. 除此之外,您还可以使用数据库锁,文件锁或其他跨进程通信机制。

Here's a description of using redis: implement mutex in node.js 这是使用redis的描述: 在node.js中实现互斥

Here's a module that offers a nodejs cross process mutex (which actually uses file locking under the covers): https://github.com/Perennials/mutex-node 这是一个提供nodejs跨进程互斥体的模块(实际上是在后台使用文件锁定): https : //github.com/Perennials/mutex-node

And another module: https://www.npmjs.com/package/rwlock 另一个模块: https : //www.npmjs.com/package/rwlock

Any of these locking mechanisms could certainly be wrapped in a promise interface. 这些锁定机制中的任何一种都可以肯定地包装在Promise接口中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM