-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_mutex_unix.go
73 lines (65 loc) · 1.39 KB
/
file_mutex_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package locker
import (
"fmt"
"os"
"path/filepath"
"sync"
"golang.org/x/sys/unix"
)
type fileMutex struct {
mutex sync.Mutex
descriptor int
}
const (
fileMutexFileExt = ".lock"
fileMutexFileMode os.FileMode = 0666
fileMutexDirMode os.FileMode = 0755
)
func NewFileMutex(address string) (Mutex, error) {
if filepath.Ext(address) != fileMutexFileExt {
return nil, fmt.Errorf(
`expected lock file path should have "%s" extension`,
fileMutexFileExt,
)
}
err := os.MkdirAll(filepath.Dir(address), fileMutexDirMode)
if err != nil {
return nil, err
}
file, err := os.OpenFile(address, os.O_CREATE|os.O_RDONLY, fileMutexFileMode)
if err != nil {
return nil, err
}
// defer file.Close() // if you close the file descriptor, then the mutex will not work
m := fileMutex{
descriptor: int(file.Fd()),
}
return &m, nil
}
func (m *fileMutex) Lock() error {
m.mutex.Lock()
err := unix.Flock(m.descriptor, unix.LOCK_EX)
if err != nil {
m.mutex.Unlock()
}
return err
}
func (m *fileMutex) TryLock() (bool, error) {
ok := m.mutex.TryLock()
if !ok {
return false, nil
}
err := unix.Flock(m.descriptor, unix.LOCK_EX|unix.LOCK_NB)
if err != nil {
m.mutex.Unlock()
if err == unix.EWOULDBLOCK {
return false, nil
}
return false, err
}
return true, nil
}
func (m *fileMutex) Unlock() error {
m.mutex.Unlock()
return unix.Flock(m.descriptor, unix.LOCK_UN)
}