gpt4 book ai didi

node.js - 如何设计可伸缩的rpc调用监听器?

转载 作者:搜寻专家 更新时间:2023-11-01 00:46:36 24 4
gpt4 key购买 nike

我必须监听 rpc 调用,将它们堆叠在某处,处理它们,然后回答。问题是它们不会一来就跑。响应是收到的每个 rpc 调用的 ACK。问题是我想以一种方式设计它,我可以让许多监听服务器写入同一个调用堆栈,并在调用时将它们堆积起来。

我的目标是接听尽可能多的电话。我该如何实现?

我的主要技术是 Perl 和 node.js,但会使用任何开源软件来完成这项任务。

最佳答案

听起来任何类型的作业队列都能满足您的需求;我个人非常喜欢使用 Redis对于这种事情。由于 Redis 列表维护插入顺序,您可以简单地 LPUSH您的 RPC 调用信息位于列表末尾,来自任意数量的监听 RPC 调用的 Web 服务器,以及其他地方(我假设在另一个进程/另一台机器上)RPOP (或 BRPOP)关闭并处理它们。

由于 Node.js 使用完全异步 IO,假设您没有在 RPC 监听器中进行大量处理(也就是说,您只监听请求、发送 ACK 并推送到 Redis),我猜是 Node 在这方面非常高效。

关于将 Redis 用于队列的旁白:如果要确保在发生灾难性故障时不会丢失作业,则需要实现更多的逻辑;来自 RPOPLPUSH文档:

Pattern: Reliable queue

Redis is often used as a messaging server to implement processing of background jobs or other kinds of messagingtasks. A simple form of queue is often obtained pushing values into alist in the producer side, and waiting for this values in the consumerside using RPOP (using polling), or BRPOP if the client is betterserved by a blocking operation.

However in this context the obtainedqueue is not reliable as messages can be lost, for example in the casethere is a network problem or if the consumer crashes just after themessage is received but it is still to process.

RPOPLPUSH (orBRPOPLPUSH for the blocking variant) offers a way to avoid thisproblem: the consumer fetches the message and at the same time pushesit into a processing list. It will use the LREM command in order toremove the message from the processing list once the message has beenprocessed.

An additional client may monitor the processing list foritems that remain there for too much time, and will push those timedout items into the queue again if needed.

关于node.js - 如何设计可伸缩的rpc调用监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10780485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com