使用 TCP 在进程间共享内存 [英] Using TCP for memory sharing across processes
问题描述
我在开始使用 nodejs
时犯了一个错误,因为没有使用 Redis
或 Memcache
或其他内存存储系统.现在,重写所有内容以在这些 API 中容纳和关联我的代码为时已晚.
I made a mistake working on nodejs
in the beginning by not utilizing Redis
or Memcache
or other memory storage systems. Now, it's far too late to be rewriting everything to accommodate and correlate my code within those API's.
然而,我最近才发现分叉过程及其有益之处;尤其是因为我正在开发游戏服务器.
However, I just recently found out about forking processes and how beneficial they can be; especially since I'm working on a gameserver.
我遇到的问题是:nodejs
中的内核之间不共享内存.. 直到我发现了一个名为 Amensia 的 TCP
内存共享模块.
The problem I have is: The memory is not shared between cores in nodejs
.. until I found a TCP
memory sharing module called Amensia.
说了这么多,我有一些关于nodejs
和tcp
的问题:
With all that said, I have some question about it pertaining to nodejs
and tcp
in general:
1) 一个 TCP 数据包的最大大小大约是 64k,所以在使用这个模块时我只能共享数据大小最大为 64k?
1) The maximum size of a TCP packet is around 64k, so when using this module I can only share data up to 64k in size?
2) 我使用全局 GAMES
和 users
对象来存储玩家数据.当玩家在地图中移动(x,y 位置)和其他动作时,这些对象会更新.通过 TCP 发送所有这些数据会导致瓶颈吗?
2) I use a global GAMES
and users
object to store player data. These objects are updated when a player moves in a map (x,y positions) and upon other actions. Would sending all this data across TCP derive into a bottleneck?
推荐答案
1) TCP 数据包大小应该没有任何问题.如果数据太大,节点将缓冲/排队,并在操作系统为其提供可写套接字的文件描述符时发送它们.只有当您每秒写入的数据超过网络带宽时,您才可能遇到性能问题.此时,Node 还将使用更多 RAM 来排队所有这些消息.
1) You should not have any problems with TCP packet size. Node will buffer/queue your data if it's too big and send them when the OS gives it a writable socket's file descriptor. You may hit performance issues only if you are writing more then your network bandwidth per second. At this point Node will also use more RAM to queue all this messages.
https://nodejs.org/api/net.html#net_socket_buffersize
2) 大多数游戏使用 TCP 或 UDP 进行实时通信.它可能是一个瓶颈,就像其他任何东西(RAM、CPU、带宽、存储)一样.在压力的某个时刻,一个或多个资源将结束/失败/表现不佳.当针对瓶颈完成所有优化并且您仍然需要向游戏服务器添加更多并发用户时,使用可以水平增长(添加更多机器)的架构通常是一个很好的做法.
2) Most games use TCP or UDP for real time communication. It can be a bottleneck, as anything else (RAM, CPU, bandwidth, storage) can. At some point of stress, one or more resources will end/fail/perform badly. It's generally a good practice to use an architecture that can grow horizontally (adding more machines) when all optimizations are done for your bottleneck and you still need to add more simultaneous users to your game server.
https://1024monkeys.wordpress.com/2014/04/01/game-servers-udp-vs-tcp/
您可能会使用 TCP 连接到 Redis 服务器(但您也可以使用 unix 套接字).
You'll probably use TCP to connect to a Redis server (but you can also use a unix socket).
如果你只需要进程间通信(而不是机器间),你应该看看集群"Node.js 核心模块.内置IPC.
If you only need inter-process communication (and not inter-machine), you should take a look at the "cluster" Node.js core module. It has built-in IPC.
这篇关于使用 TCP 在进程间共享内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!