Wizcorp / locks

Mutex locks, Read/Write locks, Condition variables and Semaphores in JavaScript
105 stars 19 forks source link

Node cluster support #13

Closed clemmy closed 7 years ago

clemmy commented 7 years ago

Just wondering if it's possible to support locks throughout the Node cluster: https://nodejs.org/api/cluster.html

Akamaozu commented 7 years ago

@clemmy I don't use locks directly through the node cluster api, but my usecase is similar enough to let me know it's possible.

tldr: run locks on one process, send messages to that process to request and release locks.

I wrote up a library that allows processes to easily send messages to / from other processes they spawn. The messages could be one to one or pubsub.

lib: https://github.com/Akamaozu/node-supe/blob/master/README.md

one to one msg: https://github.com/Akamaozu/node-supe/blob/master/README.md#send-messages

pubsub: https://github.com/Akamaozu/node-supe/blob/master/README.md#noticeboard

My particular usecase is one process running locks (lets call it the lock manager) and listening for messages from other processes. The other processes request a lock by sending a message to the manager. The manager uses locks library to handle the queuing, assignment and release of locks and simply sends a message to a process when they have acquired a lock. All the other process needs to do is send a message to release a lock when they're done with it.

The one extraneous failure mode that needs to be addressed is when a process has a lock but crashes (or shuts down). In my case, the lock manager keeps a tally of how many locks any process is currently holding and uses my lib to listen for processes with locks crashing or shutting down. If that happens, it releases all the locks that specific process had.

I run this setup in a production system and it's been rock solid so far.

Overview of my lock manager code image

Lock manager ops report generated every minute image

ronkorving commented 7 years ago

I think it's out of scope for the locks project to provide this (built-in), but I think your solution is an elegant one 👍

Akamaozu commented 7 years ago

@ronkorving I agree :)

I meant for my previous comment to be helpful info for @clemmy, not a guide for you to build it into the library.

Sorry for the confusion. I'll try to be more clear in future.

ronkorving commented 7 years ago

I wasn't confused :) It was very clear. Just saying it was a good solution to @clemmy's problem.

So, since we won't be addressing this in this project, I'll close this issue then.