2

Imagine there's a boolean status variable that is either running or stopped.

This variable is running by default and comes together with a deactivator function.

When a client calls the deactivator function, the status changes to stopped.

Calling the deactivator function returns a cancel function, which, when called, reverts the status back to running again.

If multiple clients call the deactivator function, each receives a separate cancel function. As soon as the first client calls the deactivator, the status is stopped and remains so until the last caller to deactivate has called its cancel function.

In other words, every caller can trigger a stopped, and only when no-one is voting stopped, it is running.

What is the name of that pattern?

Update: while I can implement this, I believe it has been done numerous times in the language I use (nodejs), that's why I'm looking for the name.

Update 2: I've implemented this in the meantime, but still looking for a name!

  • It's not a pattern but aren't you just describing the use of a Semaphore? – Matthew May 15 at 12:50
  • Yes, it looks very related indeed. Similar but not exactly like a lock (binary semaphore). I guess my question is, if I searched for a piece of code with above behaviour on npm, what keywords would I use? – bebbi May 15 at 12:54
  • 1
    I personally would call it an interlock pattern as you are describing the software equivalent of a mechanical/electric interlock. – David Arno May 15 at 13:58
6

What is a pattern?

A pattern is not different from any other code you use in your application. The only difference is that somebody said "this is now called [x] pattern" and then that name has been universally accepted. If I say "Visitor Pattern" to another developer, they might just know what I am talking about.

>99% of programming solutions have not been named that way. What you describe in the question probably hasn't been, either.

Which reference to use?

Don't get me wrong - other people (I myself included) have run into this same problem and came up with this same solution. Maybe somebody gave it a name, but does that already make it a pattern?

What if everybody in a small company of 15 people adopted this name for it? What if everybody in a big company with 3000 programmers did? What if they wrote a blog post and 10 000 people adopted the name? It becomes a philosophical question of what to call a pattern and what not.

Ain't nobody got time for that, so my completely arbitrary, but pragmatic way to find out the name for a pattern is to check the wikipedia page for software patterns. There are 53 different patterns described there. If it isn't in that list, I would not have any expectation that another programmer would know what I'm talking about when I utter the name. And that, IMHO, disqualifies it from being called a pattern.

Why even

Normally I would ask what you plan to gain from this anyway. In most cases, the answer amounts to "not much", but you already explained your particular goal for this in a comment:

I guess my question is, if I searched for it on npm, what keywords would I use?

Normally, I would say that this solution will always be custom-tailored to your end goal, plus it's not a big thing so you can just quickly code it yourself. But npm isn't "normally", npm has a package "is-odd" to find out if a number is odd. There might be no universally agreed upon pattern name for this, but you might find packages if you search for "interlock", as that is the name for the engineering principle behind this.

Additional Info

This answer is in the same vein as this one, asking about patterns more generally.

1

What you're probably looking for is a Consensus Algorithm.

Consensus is a problem in all distributed systems. Consider the problem of data consistency in microservices. In an ideal world, each microservice has its own data store. Before microservices, we could count on there being one, centralized data store such as a database. There was a single source of truth. But if you have multiple microservices, each with its own data store, how do you keep them in sync?

The Raft Consensus Algorithm achieves this consistency by electing a leader in a cluster of nodes. Once this leader is elected, it communicates with the other nodes, serving as the arbiter of consistency on all subsequent transactions. If the leader node goes down for some reason, a new leader is elected.

There is an online visualization demonstrating how this process works here.

rqlite is a distributed database built on top of SQLite that uses the Raft Consensus Algorithm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.