我正在实现一种作用域容器架构,以便为每个Express请求(或apollographql请求)创建一个新的container
。我有一个生命周期方法,可以在我们发送完响应后调用,这对于清理和释放内存很有好处,并且该方法可以引用我们完成服务的请求的上下文。在那个context
中,我有一个对之前创建的inversifyjs容器
的引用,因此我可以在清理方法中引用那个container
,如何删除该容器
?
function ScopedContainer(userId: number) {
const container = new Container();
container.bind<number>(TYPES.userId).toConstantValue(userId);
//rest of bindings container.bind ...
return container;
}
async({req, res}) => {
const { headers } = req;
const { userId } = headers;
const container = ScopedContainer(clientId);
const context = { container }
///...
}
willSendResponse(({container}) => {
// how to instruct inversifyjs to ditch all references to container?
// something like container.destroy?
});
我希望确保这个容器
在http请求完成后不会停留在那里,导致巨大的内存泄漏。问题是我不确定它是否会在引用计数到达零时被垃圾回收。我还使用了inversify-inject-decorators
中的lazyInject
是否有任何inversifyjs
container
API 像销毁或重置方法?