tinylibs / tinypool

🧵 A minimal and tiny Node.js Worker Thread Pool implementation (38KB)

Home Page:https://npmjs.com/package/tinypool

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add a way to recycle workers when heap limit is reached

sheremet-va opened this issue · comments

To implement vitest-dev/vitest#3203 we need a way to terminate a worker and start a new one in its place when the worker reached its memory limit. It's already possible to pass down resource limits to workers (this is a native feature), but they will fail the pool instead of recycling the worker.

Nice, Any example you got or a reproduction?

Nice, Any example you got or a reproduction?

This is a feature request. An example would be:

  • There is a pool of 5 workers
  • One of the workers gets to the maximum heap used
  • This worker is terminated and another one started to fill the gap

Ok, That's informative, thank you.

@sheremet-va have you thought about what kind of API Tinypool would provide for this option?

I'm just thinking that should there be a callback that is called when worker has finished the current task, and there user could do their own decisions whether a worker should be recycled or not. The API option would not mention heap or anything other as specific at all.

@sheremet-va have you thought about what kind of API Tinypool would provide for this option?

What I thought was just an option like maxMemoryLimitBeforeRecycle.

I'm just thinking that should there be a callback that is called when worker has finished the current task, and there user could do their own decisions whether a worker should be recycled or not. The API option would not mention heap or anything other as specific at all.

Sure, it does sound useful for some other cases, where you want to control when it happens, but the callback would be called in the main thread. This is a bit inconvenient because we will have to tell the main thread some additional information before deciding if worker should be recycled, since the memory information is available only inside the worker (which can create race conditions). The solution with maxMemoryLimitBeforeRecycle can be implemented simpler, I think.

but the callback would be called in the main thread

Right, and we cannot pass non-serializable data (functions) to workers. The maxMemoryLimitBeforeRecycle sounds simple and should be easy to implement. I'll take a look at this task.