Locker
This is the stable version.
Description
FileMutex
- To lock via operating system file- Supports functions:
- Lock
- TryLock
- Unlock
- Important:
- thread safe - You cannot take several locks from one mutex within a single running application.
- Supports functions:
Supports platforms
- unix (linux, mac)
- windows
- other
Installation
$ go get github.com/mantyr/locker
Example
Note
You will probably be protecting the entire application, rather than a single object.
type Storage struct {
mutex locker.Mutex
}
func New(fileLock string) (*Storage, error) {
mutex, err := locker.NewFileMutex(fileLock)
if err != nil {
return nil, err
}
return &Storage{
mutex: mutex,
}, nil
}
func (s *Storage) Action() (err error) {
defer s.mutex.Unlock()
err = s.mutex.Lock()
if err != nil {
return err
}
fmt.Println("action")
return nil
}
go run ./example/main.go lock ./testdata/file.lock
go run ./example/main.go try-lock ./testdata/file.lock
go run ./example/main.go multi-lock ./testdata/file.lock
go run ./example/main.go unlock ./testdata/file.lock