typescriptmemory-leaksgarbage-collectionreference-countinginversifyjs

Reset scoped container in inversifyjs


I'm implementing a scoped container architecture such that a new container is created for every Express request (or apollographql request). I have a lifecycle method that can be called after we're done sending the response, which is good for cleaning up and freeing memory, and this method can reference the context of the request we're done serving. In that context I have a reference to the inversifyjs container that I created earlier so I can reference that container in the cleanup method, how can I delete that 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?
});


I want to make sure that this container will not stay there after the http request is done, causing huge memory leak. Problem is I'm not sure it will be garbage collected as reference count reaches zeros. I'm also using lazyInject from inversify-inject-decorators

Is there any inversifyjs container API like destroy or reset method?


Solution

  • There's no persistent reference to container instance inside inversify lib. It is temporarily ref'ed by Context in _planAndResolve private method, but is released upon return so no worry.

    Conceptually, a container instance should be the top level resolver. All other services in the system are sorta "owned" by container, but nobody should own container except userland code.

    En bref, as long as you stick to the rule: only use container in your composition root, like in inversify.config.ts, you're safe.

    In your example it's less obvious how many other entities get hold to the container reference. If it's only the context object that you pass along express middlewares, then as soon as the request handling is done, context is deref'ed and so is container waiting to be GC. No further "cleanup" necessary.