From 66d29148f70a12f28b1da908d2bf44b050a40736 Mon Sep 17 00:00:00 2001 From: Erik Wilson Date: Mon, 5 Oct 2020 18:22:44 -0700 Subject: [PATCH] Add Release function for flock --- pkg/flock/flock_other.go | 7 ++++++- pkg/flock/flock_unix.go | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/pkg/flock/flock_other.go b/pkg/flock/flock_other.go index 069a5b3a2c..0082423ceb 100644 --- a/pkg/flock/flock_other.go +++ b/pkg/flock/flock_other.go @@ -19,6 +19,11 @@ limitations under the License. package flock // Acquire is not implemented on non-unix systems. -func Acquire(path string) error { +func Acquire(path string) (int, error) { + return -1, nil +} + +// Release is not implemented on non-unix systems. +func Release(lock int) error { return nil } diff --git a/pkg/flock/flock_unix.go b/pkg/flock/flock_unix.go index 3dae621b73..3dcf87dc3d 100644 --- a/pkg/flock/flock_unix.go +++ b/pkg/flock/flock_unix.go @@ -20,16 +20,17 @@ package flock import "golang.org/x/sys/unix" -// Acquire acquires a lock on a file for the duration of the process. This method -// is reentrant. -func Acquire(path string) error { - fd, err := unix.Open(path, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600) +// Acquire creates an exclusive lock on a file for the duration of the process, or until Release(d). +// This method is reentrant. +func Acquire(path string) (int, error) { + lock, err := unix.Open(path, unix.O_CREAT|unix.O_RDWR|unix.O_CLOEXEC, 0600) if err != nil { - return err + return -1, err } - - // We don't need to close the fd since we should hold - // it until the process exits. - - return unix.Flock(fd, unix.LOCK_EX) + return lock, unix.Flock(lock, unix.LOCK_EX) +} + +// Release removes an existing lock held by this process. +func Release(lock int) error { + return unix.Flock(lock, unix.LOCK_UN) }