https://github.com/brandonkramer/filelock
Cross-platform advisory file locks for single-instance services, registries, and on-disk critical sections.
https://github.com/brandonkramer/filelock
concurrency file-lock go golang library
Last synced: 17 days ago
JSON representation
Cross-platform advisory file locks for single-instance services, registries, and on-disk critical sections.
- Host: GitHub
- URL: https://github.com/brandonkramer/filelock
- Owner: brandonkramer
- License: mit
- Created: 2026-05-31T16:59:26.000Z (about 1 month ago)
- Default Branch: main
- Last Pushed: 2026-05-31T17:00:39.000Z (about 1 month ago)
- Last Synced: 2026-05-31T19:04:44.113Z (about 1 month ago)
- Topics: concurrency, file-lock, go, golang, library
- Language: Go
- Size: 53.7 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# filelock
Cross-platform advisory file locks for single-instance services, registries, and on-disk critical sections.
## Quick start
```go
// Daemon single-instance lock (non-blocking + PID marker)
release, err := filelock.Acquire(context.Background(), "/var/run/myapp/daemon.lock", filelock.WritePID())
if err != nil {
return err
}
defer release()
// Registry / state file sidecar lock
err = filelock.WithSidecar(context.Background(), "/var/lib/myapp/state.json", filelock.DefaultSidecar, func() error {
return updateState()
})
```
## Options
| Option | Use with | Effect |
|--------|----------|--------|
| `WritePID()` | `Acquire`, `With` | Write holder PID into lock file |
| `Blocking()` | `Acquire` | Wait for lock instead of returning `ErrBusy` |
| `NonBlocking()` | `With` | Try once; return `ErrBusy` when held |
| `FileMode(mode)` | both | Permission bits for new lock files |