Skip to content

Commit c541671

Browse files
authored
refactor: factorization of the reset (#79)
1 parent 1113ec8 commit c541671

File tree

4 files changed

+33
-28
lines changed

4 files changed

+33
-28
lines changed

flock.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,12 @@ func (f *Flock) ensureFhState() {
185185

186186
f.fh = nil
187187
}
188+
189+
func (f *Flock) reset() {
190+
f.l = false
191+
f.r = false
192+
193+
_ = f.fh.Close()
194+
195+
f.fh = nil
196+
}

flock_unix.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ func (f *Flock) lock(locked *bool, flag int) error {
5656
defer f.ensureFhState()
5757
}
5858

59-
if err := syscall.Flock(int(f.fh.Fd()), flag); err != nil {
59+
err := syscall.Flock(int(f.fh.Fd()), flag)
60+
if err != nil {
6061
shouldRetry, reopenErr := f.reopenFDOnError(err)
6162
if reopenErr != nil {
6263
return reopenErr
@@ -66,7 +67,8 @@ func (f *Flock) lock(locked *bool, flag int) error {
6667
return err
6768
}
6869

69-
if err = syscall.Flock(int(f.fh.Fd()), flag); err != nil {
70+
err = syscall.Flock(int(f.fh.Fd()), flag)
71+
if err != nil {
7072
return err
7173
}
7274
}
@@ -99,15 +101,12 @@ func (f *Flock) Unlock() error {
99101
}
100102

101103
// Mark the file as unlocked.
102-
if err := syscall.Flock(int(f.fh.Fd()), syscall.LOCK_UN); err != nil {
104+
err := syscall.Flock(int(f.fh.Fd()), syscall.LOCK_UN)
105+
if err != nil {
103106
return err
104107
}
105108

106-
_ = f.fh.Close()
107-
108-
f.l = false
109-
f.r = false
110-
f.fh = nil
109+
f.reset()
111110

112111
return nil
113112
}
@@ -167,7 +166,8 @@ retry:
167166
}
168167

169168
if !retried {
170-
if shouldRetry, reopenErr := f.reopenFDOnError(err); reopenErr != nil {
169+
shouldRetry, reopenErr := f.reopenFDOnError(err)
170+
if reopenErr != nil {
171171
return false, reopenErr
172172
} else if shouldRetry {
173173
retried = true
@@ -180,9 +180,8 @@ retry:
180180

181181
// reopenFDOnError determines whether we should reopen the file handle in readwrite mode and try again.
182182
// This comes from `util-linux/sys-utils/flock.c`:
183-
//
184-
// Since Linux 3.4 (commit 55725513)
185-
// Probably NFSv4 where flock() is emulated by fcntl().
183+
// > Since Linux 3.4 (commit 55725513)
184+
// > Probably NFSv4 where flock() is emulated by fcntl().
186185
func (f *Flock) reopenFDOnError(err error) (bool, error) {
187186
if !errors.Is(err, syscall.EIO) && !errors.Is(err, syscall.EBADF) {
188187
return false, nil

flock_unix_variants.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func (f *Flock) lock(locked *bool, flag lockType) error {
9999
defer f.ensureFhState()
100100
}
101101

102-
if _, err := f.doLock(waitLock, flag, true); err != nil {
102+
_, err := f.doLock(waitLock, flag, true)
103+
if err != nil {
103104
return err
104105
}
105106

@@ -137,16 +138,19 @@ func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
137138

138139
l := locks[ino]
139140

140-
if l.owner == f {
141+
switch {
142+
case l.owner == f:
141143
// This file already owns the lock, but the call may change its lock type.
142-
} else if l.owner == nil {
144+
case l.owner == nil:
143145
// No owner: it's ours now.
144146
l.owner = f
145-
} else if !blocking {
147+
148+
case !blocking:
146149
// Already owned: cannot take the lock.
147150
mu.Unlock()
148151
return false, nil
149-
} else {
152+
153+
default:
150154
// Already owned: add a channel to wait on.
151155
wait = make(chan *Flock)
152156
l.queue = append(l.queue, wait)
@@ -188,11 +192,7 @@ func (f *Flock) Unlock() error {
188192
return err
189193
}
190194

191-
_ = f.fh.Close()
192-
193-
f.l = false
194-
f.r = false
195-
f.fh = nil
195+
f.reset()
196196

197197
return nil
198198
}

flock_windows.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package flock
77

88
import (
9+
"errors"
910
"syscall"
1011
)
1112

@@ -85,11 +86,7 @@ func (f *Flock) Unlock() error {
8586
return errNo
8687
}
8788

88-
_ = f.fh.Close()
89-
90-
f.l = false
91-
f.r = false
92-
f.fh = nil
89+
f.reset()
9390

9491
return nil
9592
}
@@ -137,7 +134,7 @@ func (f *Flock) try(locked *bool, flag uint32) (bool, error) {
137134

138135
_, errNo := lockFileEx(syscall.Handle(f.fh.Fd()), flag|winLockfileFailImmediately, 0, 1, 0, &syscall.Overlapped{})
139136
if errNo > 0 {
140-
if errNo == ErrorLockViolation || errNo == syscall.ERROR_IO_PENDING {
137+
if errors.Is(errNo, ErrorLockViolation) || errors.Is(errNo, syscall.ERROR_IO_PENDING) {
141138
return false, nil
142139
}
143140

0 commit comments

Comments
 (0)