Richard Bucker

Lockfiles

Posted at — Jan 12, 2015

Inspired by man 1 flock I have decided to build my own flock utility in golang. After creating a public git repo on bitbucket I started thinking about the details. (a) use stdlib only (b) keep it simple © offer a command line and a package.

Nothing new there, but then…

Now the question becomes; should I always have a lock file? Every time I run any application should I have a lock file? Should that lock file prevent multiple instances of THIS version of the tool or any version of the tool. Or should the lock file act as a sort of semaphore indicating that there is an application of this type running?

I ended up with a few choices:

$ myapp -pidlockfile
Use the program name arg[0] and the current pid.
$ myapp -buildlockfile
Use the program name and CI build number.
$ myapp -lockfile
Use the program name prevent multiple instances of the same application on the same machine
$ myapp -nolockfile
Do not use a lock file.

One thing to keep in mind is that if you use fleet to launch your containers, and in turn your applications, then this feature is not really needed. It’s not likely that you’ll launch multiple instances… but then again it’s not going to cost you anything to combine them. Also, fleet will handle the instance generation over the network instead of a single machine.