minima-global / Minima

The Minima protocol Java libraries and application
Apache License 2.0
126 stars 148 forks source link

DRAFT add queue and thread for processing mRunnables asynchronously #618

Open mihbor opened 5 months ago

mihbor commented 5 months ago

I discovered some issues caused by long running service.js code. The problem seems to be the MDSManager is single-threaded. When one Minidapp's service.js is doing something for a long time, blocking the thread, messages aren't delivered to other minidapps and the MDS Hub stops functioning corretly as well. Here's what I observed happens when service.js is running for a long time:

I can't see a simple fix for this. Ideally, I imagine each mRunnable should have its own queue of commands to process and a (small) pool of worker threads should be picking the mRunnables with non-empty queues and processing them asynchronously, while the MDSManager would only add new tasks to the queues. A much simpler approach that fixes the worst issues of the hub itself not working correctly would be to have at least a single queue of pending tasks for all the mRunnables and one background thread to process them.

This PR is just a draft, a WIP, to show what the approach could be, to begin with. It's far from ideal but I tested it (on desktop) to fix the hub missbehaving at least, as a start.