在决议/拒绝后,承诺垃圾的解决/拒绝功能?

时间:2020-12-14 23:55:09

I'm trying to implement RPC broker. The broker takes commands, sends them to the RPC servers, waits for result and then sends the result to the client. My approach is like this pseudocode:

我正在尝试实现RPC代理。代理接收命令,将它们发送到RPC服务器,等待结果,然后将结果发送给客户端。我的方法是这样的伪代码:

function onClientCommand(cmd) {
    sendCommandToRPCServer.then(function(result){
        returnResultToClient(cmd.client,result)
    })  
}  

As you can see sendCommandToRPCServer returns a promise. I'm expecting this function to be called very frequently (several thousands calls per second). When the system get under heavy load, RPC servers get slow and there will be more and more unresolved promises in Broker memory.

您可以看到sendCommandToRPCServer返回一个承诺。我期望这个函数被频繁调用(每秒数千次调用)。当系统负载过重时,RPC服务器会变慢,在代理内存中会有越来越多未解决的承诺。

So my question is:

我的问题是:

If the Broker rejects promises after some timeout, will this remove promises (e.g. its resolve and reject functions with their contexts, there is no reference to that promise) from memory? I know I can solve this by array of callbacks and periodically clear old items of it, but "promise-like" approach is better for me, because sometimes broker needs to wait for several RPC calls and then return result to client and this is easier with promises.

如果代理在某个超时之后拒绝了承诺,那么它会从内存中删除承诺(例如,它的解析和拒绝功能与上下文无关)吗?我知道我可以通过回调数组来解决这个问题,并定期清除它的旧项,但是“类似于promip的”方法对我来说更好,因为有时候代理需要等待几个RPC调用,然后将结果返回给客户端,这对于承诺来说更容易。

1 个解决方案

#1


2  

No, garbage collection does not work any different for them. A function is, like any other object, garbage-collected when nothing references it any more.

不,垃圾收集对他们没有任何不同。与任何其他对象一样,当不再有引用时,函数也是垃圾收集的。

There are of course some "hidden" references in the promise implementation:

当然,承诺实现中有一些“隐藏”的引用:

  • resolving functions (resolve and reject) do reference the promise
  • 解析函数(解析和拒绝)确实引用了承诺
  • the promise references all then callbacks installed on it
  • 承诺引用所有安装在它上的回调
  • the installed handlers reference the promise created by the then() call
  • 已安装的处理程序引用then()调用创建的承诺

A good implementation does remove the reference from the resolving functions to the promise when one of them was called, and it does remove the references from the promise to the handlers when the promise was settled.

一个好的实现会在其中一个承诺被调用时从解析函数中删除对承诺的引用,并且在承诺被解决时从承诺中删除对处理程序的引用。

Regarding your specific use case, storing the resolving functions as callbacks while you are waiting for the respective response is perfectly fine. Have a look at this Queue implementation for an example. Just make sure to drop the functions after you used them to avoid memory leaks.

对于您的特定用例,在等待相应的响应时将解析函数存储为回调是完全可以的。看一下这个队列实现的例子。只要确保在使用函数之后删除它们,以避免内存泄漏。

#1


2  

No, garbage collection does not work any different for them. A function is, like any other object, garbage-collected when nothing references it any more.

不,垃圾收集对他们没有任何不同。与任何其他对象一样,当不再有引用时,函数也是垃圾收集的。

There are of course some "hidden" references in the promise implementation:

当然,承诺实现中有一些“隐藏”的引用:

  • resolving functions (resolve and reject) do reference the promise
  • 解析函数(解析和拒绝)确实引用了承诺
  • the promise references all then callbacks installed on it
  • 承诺引用所有安装在它上的回调
  • the installed handlers reference the promise created by the then() call
  • 已安装的处理程序引用then()调用创建的承诺

A good implementation does remove the reference from the resolving functions to the promise when one of them was called, and it does remove the references from the promise to the handlers when the promise was settled.

一个好的实现会在其中一个承诺被调用时从解析函数中删除对承诺的引用,并且在承诺被解决时从承诺中删除对处理程序的引用。

Regarding your specific use case, storing the resolving functions as callbacks while you are waiting for the respective response is perfectly fine. Have a look at this Queue implementation for an example. Just make sure to drop the functions after you used them to avoid memory leaks.

对于您的特定用例,在等待相应的响应时将解析函数存储为回调是完全可以的。看一下这个队列实现的例子。只要确保在使用函数之后删除它们,以避免内存泄漏。