werf / lockgate

Lockgate is a cross-platform distributed locking library for Go. Supports distributed locks backed by Kubernetes or HTTP lock server. Supports conventional OS file locks.
Apache License 2.0
256 stars 15 forks source link

Simple CLI integration #36

Open splitice opened 2 years ago

splitice commented 2 years ago

Thank you for your hard work. Been reading through this and the few other implementations of distributed locking systems. A uniquely simple design in this case. Effectively you take the lock for (default) 10 seconds and keep renewing it preventing expiration (every 3 seconds). Simple design and K8S services while they can be a bit jittery should be reliable on that scale. Competition between acquiring workers being handled by a race to insert.

I don't do anything normally with golang so correct me if I am wrong.

Any chance that an (example?) of a CLI application could be developed that could be usable from scripting languages? This would make your work accessible from sh/bash (or any other scripting language supporting shell commands - i.e most of them)? I suspect even languages like PHP and python could make use of it via proc open type interfaces.

My thoughts regarding API:

#!/bin/bash

keyname="our key"

# take lock $keyname
k8lock $keyname &
lockpid=$!

do_stuff

# signal for clean exit
kill -sHUP $lockpid
wait $lockpid

distlock should also monitor it's parent pid for exit (and release the lock accordingly i.e in case of crash).

In C I would do this with a signal handler on SIGHUP and use PR_SET_PDEATHSIG to ensure a SIGHUP is received on parent death for that graceful cleanup.

distorhead commented 2 years ago

Hi, thanks for your feedback!

I think it would be great to implement such CLI app. Flock unix util could be used as another example CLI.

The only thing: it is important to implement on-lost-lease handler for such CLI command, in the simple/universal case this handler should just crash whole script immediately, in some cases this handler could revert back introduced changes then crash (depends on an application logic).

splitice commented 2 years ago

I guess if the lock cli exits early thats a lost lock. Although that really shouldnt be possible and many CLI applications are not easily able to check the statuses of their children (and shouldnt be expected to). I don't think the child sending signals to the parent is a good idea (e.g to interrupt / terminate it) at-least not by default.

I'm pretty much thinking of usage like a simple distributed flock 👍

cwarden commented 4 months ago

Here's a simple cli I built using lockgate: https://github.com/octoberswimmer/deploylock