mirror of
https://github.com/prometheus/prometheus.git
synced 2024-11-10 23:54:05 -08:00
baca6faa1c
This mimics the locking leveldb is performing anyway. Advantages of doing it separately: - Should we ever replace the leveldb implementation by one without double-start protection, we are still good. - In contrast to leveldb, the new code creates a meaningful error message.
38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
// Package flock provides portable file locking. It is essentially ripped out
|
|
// from the code of github.com/syndtr/goleveldb. Strange enough that the
|
|
// standard library does not provide this functionality. Once this package has
|
|
// proven to work as expected, we should probably turn it into a separate
|
|
// general purpose package for humanity.
|
|
package flock
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Releaser provides the Release method to release a file lock.
|
|
type Releaser interface {
|
|
Release() error
|
|
}
|
|
|
|
// New locks the file with the provided name. If the file does not exist, it is
|
|
// created. The returned Releaser is used to release the lock. The returned
|
|
// boolean is true if the file to lock already existed. A non-nil error is
|
|
// returned if the locking has failed. Neither this function nor the returned
|
|
// Releaser is goroutine-safe.
|
|
func New(fileName string) (Releaser, bool, error) {
|
|
if err := os.MkdirAll(filepath.Dir(fileName), 0755); err != nil {
|
|
return nil, false, err
|
|
}
|
|
|
|
_, err := os.Stat(fileName)
|
|
existed := err == nil
|
|
|
|
lock, err := newLock(fileName)
|
|
if err != nil {
|
|
return nil, existed, err
|
|
}
|
|
|
|
return lock, existed, nil
|
|
}
|