package flock
import "github.com/gofrs/flock"
Package flock implements a thread-safe interface for file locking. It also includes a non-blocking TryLock() function to allow locking without blocking execution.
Package flock is released under the BSD 3-Clause License. See the LICENSE file for more details.
While using this library, remember that the locking behaviors are not guaranteed to be the same on each platform. For example, some UNIX-like operating systems will transparently convert a shared lock to an exclusive lock. If you Unlock() the flock from a location where you believe that you have the shared lock, you may accidentally drop the exclusive lock.
Index ¶
- Constants
- type Flock
- func New(path string, opts ...Option) *Flock
- func NewFlock(path string) *Flock
- func (f *Flock) Close() error
- func (f *Flock) Lock() error
- func (f *Flock) Locked() bool
- func (f *Flock) Path() string
- func (f *Flock) RLock() error
- func (f *Flock) RLocked() bool
- func (f *Flock) String() string
- func (f *Flock) TryLock() (bool, error)
- func (f *Flock) TryLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)
- func (f *Flock) TryRLock() (bool, error)
- func (f *Flock) TryRLockContext(ctx context.Context, retryDelay time.Duration) (bool, error)
- func (f *Flock) Unlock() error
- type Option
Examples ¶
Constants ¶
ErrorLockViolation is the error code returned from the Windows syscall when a lock would block, and you ask to fail immediately.
Types ¶
type Flock ¶
type Flock struct {
// contains filtered or unexported fields
}
Flock is the struct type to handle file locking. All fields are unexported, with access to some of the fields provided by getter methods (Path() and Locked()).
func New ¶
New returns a new instance of *Flock. The only parameter it takes is the path to the desired lockfile.
func NewFlock ¶
NewFlock returns a new instance of *Flock. The only parameter it takes is the path to the desired lockfile.
Deprecated: Use New instead.
func (*Flock) Close ¶
Close is equivalent to calling Unlock.
This will release the lock and close the underlying file descriptor. It will not remove the file from disk, that's up to your application.
func (*Flock) Lock ¶
Lock is a blocking call to try and take an exclusive file lock. It will wait until it is able to obtain the exclusive file lock. It's recommended that TryLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
If we are already locked, this function short-circuits and returns immediately assuming it can take the mutex lock.
func (*Flock) Locked ¶
Locked returns the lock state (locked: true, unlocked: false).
Warning: by the time you use the returned value, the state may have changed.
Code:play
Output:Example¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/gofrs/flock"
)
func main() {
f := flock.New(filepath.Join(os.TempDir(), "go-lock.lock"))
_, err := f.TryLock()
if err != nil {
// handle locking error
panic(err)
}
fmt.Printf("locked: %v\n", f.Locked())
err = f.Unlock()
if err != nil {
// handle locking error
panic(err)
}
fmt.Printf("locked: %v\n", f.Locked())
}
locked: true
locked: false
func (*Flock) Path ¶
Path returns the path as provided in NewFlock().
func (*Flock) RLock ¶
RLock is a blocking call to try and take a shared file lock. It will wait until it is able to obtain the shared file lock. It's recommended that TryRLock() be used over this function. This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
If we are already locked, this function short-circuits and returns immediately assuming it can take the mutex lock.
func (*Flock) RLocked ¶
RLocked returns the read lock state (locked: true, unlocked: false).
Warning: by the time you use the returned value, the state may have changed.
func (*Flock) String ¶
func (*Flock) TryLock ¶
TryLock is the preferred function for taking an exclusive file lock. This function does take a RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.
The actual file lock is non-blocking.
If we are unable to get the exclusive file lock,
the function will return false instead of waiting for the lock.
If we get the lock, we also set the *Flock instance as being exclusive-locked.
Code:play
Example¶
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/gofrs/flock"
)
func main() {
// should probably put these in /var/lock
f := flock.New(filepath.Join(os.TempDir(), "go-lock.lock"))
locked, err := f.TryLock()
if err != nil {
// handle locking error
panic(err)
}
if locked {
fmt.Printf("path: %s; locked: %v\n", f.Path(), f.Locked())
if err := f.Unlock(); err != nil {
// handle unlock error
panic(err)
}
}
fmt.Printf("path: %s; locked: %v\n", f.Path(), f.Locked())
}
func (*Flock) TryLockContext ¶
TryLockContext repeatedly tries to take an exclusive lock until one of the conditions is met:
- TryLock succeeds
- TryLock fails with error
- Context Done channel is closed.
Code:play
Example¶
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"time"
"github.com/gofrs/flock"
)
func main() {
// should probably put these in /var/lock
f := flock.New(filepath.Join(os.TempDir(), "go-lock.lock"))
lockCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
locked, err := f.TryLockContext(lockCtx, 678*time.Millisecond)
if err != nil {
// handle locking error
panic(err)
}
if locked {
fmt.Printf("path: %s; locked: %v\n", f.Path(), f.Locked())
if err := f.Unlock(); err != nil {
// handle unlock error
panic(err)
}
}
fmt.Printf("path: %s; locked: %v\n", f.Path(), f.Locked())
}
func (*Flock) TryRLock ¶
TryRLock is the preferred function for taking a shared file lock. This function does take a RW-mutex lock before it tries to lock the file, so there is the possibility that this function may block for a short time if another goroutine is trying to take any action.
The actual file lock is non-blocking. If we are unable to get the shared file lock, the function will return false instead of waiting for the lock. If we get the lock, we also set the *Flock instance as being shared-locked.
func (*Flock) TryRLockContext ¶
TryRLockContext repeatedly tries to take a shared lock until one of the conditions is met: - TryRLock succeeds - TryRLock fails with error - Context Done channel is closed.
func (*Flock) Unlock ¶
Unlock is a function to unlock the file. This file takes a RW-mutex lock, so while it is running the Locked() and RLocked() functions will be blocked.
This function short-circuits if we are unlocked already. If not, it calls UnlockFileEx() on the file and closes the file descriptor. It does not remove the file from disk. It's up to your application to do.
type Option ¶
type Option func(f *Flock)
func SetFlag ¶
SetFlag sets the flag used to create/open the file.
func SetPermissions ¶
SetPermissions sets the OS permissions to set on the file.
Source Files ¶
- Version
- v0.12.1 (latest)
- Published
- Jul 22, 2024
- Platform
- windows/amd64
- Imports
- 8 packages
- Last checked
- 1 month ago –
Tools for package owners.