run_exclusive icon indicating copy to clipboard operation
run_exclusive copied to clipboard

Does it work on distributed environments?

Open pedrolcn opened this issue 5 years ago • 1 comments

Hi, awesome looking project.

I looked through the documentation/examples and through the code and it doesn't seem to address distributed environments use cases. (eg. when we want a function execution to be exclusive not only on a single instance but across many instances of node which may or may not be running on the same machine).

If it does work on distributed environments, how can it be done? If it does not, is it included in the roadmap? What are the data-structures which would have to be made distributed-safe and how can I help ? Thanks

pedrolcn avatar Feb 17 '20 13:02 pedrolcn

Hey, Thank you for the feedback. No, it's currently not supported but I can definitively see the use case.
Implementing it would require toasting everything but the core API design though...
I think the better approach would be to solve the problem at a lower level in a different project that would be like DirtyHairy/async-mutex and then use this project as a dependency for run-exclusive.

To keep it platform agnostic the subproject should define an interface like:

type DistributedMutex= {
    aquireLock(onLockAquired: (releaseLock: ()=> void)): void;
    isLocked(): boolean;
};
//PS: Callback instead of promises to allow for syncronously releasing the lock whenever possible.

And use this interface as dependency injection.
A bit like express session where you have to provide your own store.
I can see two main implementations of DistribudedMutexone relying on a database connection to be 100% distributed across node process AND across hosts.
And another implementation based on UNIX socket that would be easier to set up but would only enforce the lock across node process running on a single host.

Unfortunately, I have too much on my hands right now to undertake this. It would be very dope though and if it existed I would use it. Of course, I will merge any PR that would be backward compatible.

Cheers

garronej avatar Feb 17 '20 17:02 garronej