| 1 |
// Copyright 2015 Tim Heckman. All rights reserved. |
| 2 |
// Use of this source code is governed by the BSD 3-Clause |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package flock implements a thread-safe interface for file locking. |
| 6 |
// It also includes a non-blocking TryLock() function to allow locking |
| 7 |
// without blocking execution. |
| 8 |
// |
| 9 |
// Package flock is released under the BSD 3-Clause License. See the LICENSE file |
| 10 |
// for more details. |
| 11 |
// |
| 12 |
// While using this library, remember that the locking behaviors are not |
| 13 |
// guaranteed to be the same on each platform. For example, some UNIX-like |
| 14 |
// operating systems will transparently convert a shared lock to an exclusive |
| 15 |
// lock. If you Unlock() the flock from a location where you believe that you |
| 16 |
// have the shared lock, you may accidentally drop the exclusive lock. |
| 17 |
package flock |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"os" |
| 22 |
"sync" |
| 23 |
"time" |
| 24 |
) |
| 25 |
|
| 26 |
// Flock is the struct type to handle file locking. All fields are unexported, |
| 27 |
// with access to some of the fields provided by getter methods (Path() and Locked()). |
| 28 |
type Flock struct { |
| 29 |
path string |
| 30 |
m sync.RWMutex |
| 31 |
fh *os.File |
| 32 |
l bool |
| 33 |
r bool |
| 34 |
} |
| 35 |
|
| 36 |
// New returns a new instance of *Flock. The only parameter |
| 37 |
// it takes is the path to the desired lockfile. |
| 38 |
func New(path string) *Flock { |
| 39 |
return &Flock{path: path} |
| 40 |
} |
| 41 |
|
| 42 |
// NewFlock returns a new instance of *Flock. The only parameter |
| 43 |
// it takes is the path to the desired lockfile. |
| 44 |
// |
| 45 |
// Deprecated: Use New instead. |
| 46 |
func NewFlock(path string) *Flock { |
| 47 |
return New(path) |
| 48 |
} |
| 49 |
|
| 50 |
// Close is equivalent to calling Unlock. |
| 51 |
// |
| 52 |
// This will release the lock and close the underlying file descriptor. |
| 53 |
// It will not remove the file from disk, that's up to your application. |
| 54 |
func (f *Flock) Close() error { |
| 55 |
return f.Unlock() |
| 56 |
} |
| 57 |
|
| 58 |
// Fh returns the lock file |
| 59 |
func (f *Flock) Fh() *os.File { |
| 60 |
return f.fh |
| 61 |
} |
| 62 |
|
| 63 |
// Path returns the path as provided in NewFlock(). |
| 64 |
func (f *Flock) Path() string { |
| 65 |
return f.path |
| 66 |
} |
| 67 |
|
| 68 |
// Locked returns the lock state (locked: true, unlocked: false). |
| 69 |
// |
| 70 |
// Warning: by the time you use the returned value, the state may have changed. |
| 71 |
func (f *Flock) Locked() bool { |
| 72 |
f.m.RLock() |
| 73 |
defer f.m.RUnlock() |
| 74 |
return f.l |
| 75 |
} |
| 76 |
|
| 77 |
// RLocked returns the read lock state (locked: true, unlocked: false). |
| 78 |
// |
| 79 |
// Warning: by the time you use the returned value, the state may have changed. |
| 80 |
func (f *Flock) RLocked() bool { |
| 81 |
f.m.RLock() |
| 82 |
defer f.m.RUnlock() |
| 83 |
return f.r |
| 84 |
} |
| 85 |
|
| 86 |
func (f *Flock) String() string { |
| 87 |
return f.path |
| 88 |
} |
| 89 |
|
| 90 |
// TryLockContext repeatedly tries to take an exclusive lock until one of the |
| 91 |
// conditions is met: TryLock succeeds, TryLock fails with error, or Context |
| 92 |
// Done channel is closed. |
| 93 |
func (f *Flock) TryLockContext(ctx context.Context, retryDelay time.Duration) (bool, error) { |
| 94 |
return tryCtx(ctx, f.TryLock, retryDelay) |
| 95 |
} |
| 96 |
|
| 97 |
// TryRLockContext repeatedly tries to take a shared lock until one of the |
| 98 |
// conditions is met: TryRLock succeeds, TryRLock fails with error, or Context |
| 99 |
// Done channel is closed. |
| 100 |
func (f *Flock) TryRLockContext(ctx context.Context, retryDelay time.Duration) (bool, error) { |
| 101 |
return tryCtx(ctx, f.TryRLock, retryDelay) |
| 102 |
} |
| 103 |
|
| 104 |
func tryCtx(ctx context.Context, fn func() (bool, error), retryDelay time.Duration) (bool, error) { |
| 105 |
if ctx.Err() != nil { |
| 106 |
return false, ctx.Err() |
| 107 |
} |
| 108 |
for { |
| 109 |
if ok, err := fn(); ok || err != nil { |
| 110 |
return ok, err |
| 111 |
} |
| 112 |
select { |
| 113 |
case <-ctx.Done(): |
| 114 |
return false, ctx.Err() |
| 115 |
case <-time.After(retryDelay): |
| 116 |
// try again |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
func (f *Flock) setFh() error { |
| 122 |
fh, err := os.OpenFile(f.path, os.O_CREATE|os.O_RDWR, os.FileMode(0600)) |
| 123 |
if err == nil { |
| 124 |
f.fh = fh |
| 125 |
} |
| 126 |
return err |
| 127 |
} |
| 128 |
|
| 129 |
// ensure the file handle is closed if no lock is held |
| 130 |
func (f *Flock) ensureFhState() { |
| 131 |
if !f.l && !f.r && f.fh != nil { |
| 132 |
f.fh.Close() |
| 133 |
f.fh = nil |
| 134 |
} |
| 135 |
} |