https://github.com/ucloud/go-lockfile
A Linux go library to lock cooperating processes based on syscall `flock`
https://github.com/ucloud/go-lockfile
Last synced: 8 months ago
JSON representation
A Linux go library to lock cooperating processes based on syscall `flock`
- Host: GitHub
- URL: https://github.com/ucloud/go-lockfile
- Owner: ucloud
- License: mit
- Created: 2022-10-18T06:08:06.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-10-18T11:11:40.000Z (over 3 years ago)
- Last Synced: 2025-05-26T19:05:47.004Z (9 months ago)
- Language: Go
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-lockfile
[](https://godoc.org/github.com/ucloud/go-lockfile) [](https://goreportcard.com/report/github.com/ucloud/go-lockfile)
A Linux go library to lock cooperating processes based on syscall [flock](https://man7.org/linux/man-pages/man2/flock.2.html).
## Install
```shell
go get github.com/ucloud/go-lockfile
```
## Usage
A simple example:
```go
package main
import (
"errors"
"fmt"
"os"
"time"
"github.com/ucloud/go-lockfile"
)
func main() {
fl := lockfile.New("busy.lock")
var err error
for i := 0; i < 20; i++ {
err = fl.TryLock()
if err == nil {
break
}
time.Sleep(time.Millisecond * 50)
}
if err != nil {
if errors.Is(err, lockfile.ErrBusy) {
fmt.Println("The lock is busy")
} else {
fmt.Printf("Unexpected error: %v\n", err)
}
os.Exit(1)
}
fmt.Println("lock!")
// Handle your logic
time.Sleep(time.Second)
err = fl.Unlock()
if err != nil {
fmt.Printf("failed to unlock: %v\n", err)
os.Exit(1)
}
}
```
## Documentation
See: [go-lockfile package](https://pkg.go.dev/github.com/ucloud/go-lockfile)