Skip to content

Commit c9b0b98

Browse files
committed
chore: apply semantic line breaks
1 parent 016605a commit c9b0b98

File tree

5 files changed

+135
-127
lines changed

5 files changed

+135
-127
lines changed

flock.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,18 @@ func (f *Flock) String() string {
127127
return f.path
128128
}
129129

130-
// TryLockContext repeatedly tries to take an exclusive lock until one of the
131-
// conditions is met: TryLock succeeds, TryLock fails with error, or Context
132-
// Done channel is closed.
130+
// TryLockContext repeatedly tries to take an exclusive lock until one of the conditions is met:
131+
// - TryLock succeeds
132+
// - TryLock fails with error
133+
// - Context Done channel is closed.
133134
func (f *Flock) TryLockContext(ctx context.Context, retryDelay time.Duration) (bool, error) {
134135
return tryCtx(ctx, f.TryLock, retryDelay)
135136
}
136137

137-
// TryRLockContext repeatedly tries to take a shared lock until one of the
138-
// conditions is met: TryRLock succeeds, TryRLock fails with error, or Context
139-
// Done channel is closed.
138+
// TryRLockContext repeatedly tries to take a shared lock until one of the conditions is met:
139+
// - TryRLock succeeds
140+
// - TryRLock fails with error
141+
// - Context Done channel is closed.
140142
func (f *Flock) TryRLockContext(ctx context.Context, retryDelay time.Duration) (bool, error) {
141143
return tryCtx(ctx, f.TryRLock, retryDelay)
142144
}

flock_unix.go

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,29 @@ import (
1313
"syscall"
1414
)
1515

16-
// Lock is a blocking call to try and take an exclusive file lock. It will wait
17-
// until it is able to obtain the exclusive file lock. It's recommended that
18-
// TryLock() be used over this function. This function may block the ability to
19-
// query the current Locked() or RLocked() status due to a RW-mutex lock.
16+
// Lock is a blocking call to try and take an exclusive file lock.
17+
// It will wait until it is able to obtain the exclusive file lock.
18+
// It's recommended that TryLock() be used over this function.
19+
// This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
2020
//
21-
// If we are already exclusive-locked, this function short-circuits and returns
22-
// immediately assuming it can take the mutex lock.
21+
// If we are already exclusive-locked,
22+
// this function short-circuits and returns immediately assuming it can take the mutex lock.
2323
//
24-
// If the *Flock has a shared lock (RLock), this may transparently replace the
25-
// shared lock with an exclusive lock on some UNIX-like operating systems. Be
26-
// careful when using exclusive locks in conjunction with shared locks
27-
// (RLock()), because calling Unlock() may accidentally release the exclusive
28-
// lock that was once a shared lock.
24+
// If the *Flock has a shared lock (RLock),
25+
// this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems.
26+
// Be careful when using exclusive locks in conjunction with shared locks (RLock()),
27+
// because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
2928
func (f *Flock) Lock() error {
3029
return f.lock(&f.l, syscall.LOCK_EX)
3130
}
3231

33-
// RLock is a blocking call to try and take a shared file lock. It will wait
34-
// until it is able to obtain the shared file lock. It's recommended that
35-
// TryRLock() be used over this function. This function may block the ability to
36-
// query the current Locked() or RLocked() status due to a RW-mutex lock.
32+
// RLock is a blocking call to try and take a shared file lock.
33+
// It will wait until it is able to obtain the shared file lock.
34+
// It's recommended that TryRLock() be used over this function.
35+
// This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
3736
//
38-
// If we are already shared-locked, this function short-circuits and returns
39-
// immediately assuming it can take the mutex lock.
37+
// If we are already shared-locked,
38+
// this function short-circuits and returns immediately assuming it can take the mutex lock.
4039
func (f *Flock) RLock() error {
4140
return f.lock(&f.r, syscall.LOCK_SH)
4241
}
@@ -77,27 +76,29 @@ func (f *Flock) lock(locked *bool, flag int) error {
7776
return nil
7877
}
7978

80-
// Unlock is a function to unlock the file. This file takes a RW-mutex lock, so
81-
// while it is running the Locked() and RLocked() functions will be blocked.
79+
// Unlock is a function to unlock the file.
80+
// This file takes a RW-mutex lock,
81+
// so while it is running the Locked() and RLocked() functions will be blocked.
8282
//
83-
// This function short-circuits if we are unlocked already. If not, it calls
84-
// syscall.LOCK_UN on the file and closes the file descriptor. It does not
85-
// remove the file from disk. It's up to your application to do.
83+
// This function short-circuits if we are unlocked already.
84+
// If not, it calls syscall.LOCK_UN on the file and closes the file descriptor.
85+
// It does not remove the file from disk. It's up to your application to do.
8686
//
87-
// Please note, if your shared lock became an exclusive lock this may
88-
// unintentionally drop the exclusive lock if called by the consumer that
89-
// believes they have a shared lock. Please see Lock() for more details.
87+
// Please note,
88+
// if your shared lock became an exclusive lock,
89+
// this may unintentionally drop the exclusive lock if called by the consumer that believes they have a shared lock.
90+
// Please see Lock() for more details.
9091
func (f *Flock) Unlock() error {
9192
f.m.Lock()
9293
defer f.m.Unlock()
9394

94-
// if we aren't locked or if the lockfile instance is nil
95-
// just return a nil error because we are unlocked
95+
// If we aren't locked or if the lockfile instance is nil
96+
// just return a nil error because we are unlocked.
9697
if (!f.l && !f.r) || f.fh == nil {
9798
return nil
9899
}
99100

100-
// mark the file as unlocked
101+
// Mark the file as unlocked.
101102
if err := syscall.Flock(int(f.fh.Fd()), syscall.LOCK_UN); err != nil {
102103
return err
103104
}
@@ -111,26 +112,28 @@ func (f *Flock) Unlock() error {
111112
return nil
112113
}
113114

114-
// TryLock is the preferred function for taking an exclusive file lock. This
115-
// function takes an RW-mutex lock before it tries to lock the file, so there is
116-
// the possibility that this function may block for a short time if another
117-
// goroutine is trying to take any action.
115+
// TryLock is the preferred function for taking an exclusive file lock.
116+
// This function takes an RW-mutex lock before it tries to lock the file,
117+
// so there is the possibility that this function may block for a short time
118+
// if another goroutine is trying to take any action.
118119
//
119-
// The actual file lock is non-blocking. If we are unable to get the exclusive
120-
// file lock, the function will return false instead of waiting for the lock. If
121-
// we get the lock, we also set the *Flock instance as being exclusive-locked.
120+
// The actual file lock is non-blocking.
121+
// If we are unable to get the exclusive file lock,
122+
// the function will return false instead of waiting for the lock.
123+
// If we get the lock, we also set the *Flock instance as being exclusive-locked.
122124
func (f *Flock) TryLock() (bool, error) {
123125
return f.try(&f.l, syscall.LOCK_EX)
124126
}
125127

126-
// TryRLock is the preferred function for taking a shared file lock. This
127-
// function takes an RW-mutex lock before it tries to lock the file, so there is
128-
// the possibility that this function may block for a short time if another
129-
// goroutine is trying to take any action.
128+
// TryRLock is the preferred function for taking a shared file lock.
129+
// This function takes an RW-mutex lock before it tries to lock the file,
130+
// so there is the possibility that this function may block for a short time
131+
// if another goroutine is trying to take any action.
130132
//
131-
// The actual file lock is non-blocking. If we are unable to get the shared file
132-
// lock, the function will return false instead of waiting for the lock. If we
133-
// get the lock, we also set the *Flock instance as being share-locked.
133+
// The actual file lock is non-blocking.
134+
// If we are unable to get the shared file lock,
135+
// the function will return false instead of waiting for the lock.
136+
// If we get the lock, we also set the *Flock instance as being share-locked.
134137
func (f *Flock) TryRLock() (bool, error) {
135138
return f.try(&f.r, syscall.LOCK_SH)
136139
}
@@ -175,8 +178,8 @@ retry:
175178
return false, err
176179
}
177180

178-
// reopenFDOnError determines whether we should reopen the file handle
179-
// in readwrite mode and try again. This comes from util-linux/sys-utils/flock.c:
181+
// reopenFDOnError determines whether we should reopen the file handle in readwrite mode and try again.
182+
// This comes from `util-linux/sys-utils/flock.c`:
180183
//
181184
// Since Linux 3.4 (commit 55725513)
182185
// Probably NFSv4 where flock() is emulated by fcntl().

flock_unix_variants.go

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// Use of this source code is governed by a BSD-style
88
// license that can be found in the LICENSE file.
99

10-
// This code implements the filelock API using POSIX 'fcntl' locks, which attach
11-
// to an (inode, process) pair rather than a file descriptor. To avoid unlocking
12-
// files prematurely when the same file is opened through different descriptors,
10+
// This code implements the filelock API using POSIX 'fcntl' locks,
11+
// which attach to an (inode, process) pair rather than a file descriptor.
12+
// To avoid unlocking files prematurely when the same file is opened through different descriptors,
1313
// we allow only one read-lock at a time.
1414
//
1515
// This code is adapted from the Go package:
@@ -56,30 +56,29 @@ var (
5656
locks = map[inode]inodeLock{}
5757
)
5858

59-
// Lock is a blocking call to try and take an exclusive file lock. It will wait
60-
// until it is able to obtain the exclusive file lock. It's recommended that
61-
// TryLock() be used over this function. This function may block the ability to
62-
// query the current Locked() or RLocked() status due to a RW-mutex lock.
59+
// Lock is a blocking call to try and take an exclusive file lock.
60+
// It will wait until it is able to obtain the exclusive file lock.
61+
// It's recommended that TryLock() be used over this function.
62+
// This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
6363
//
64-
// If we are already exclusive-locked, this function short-circuits and returns
65-
// immediately assuming it can take the mutex lock.
64+
// If we are already exclusive-locked, this function short-circuits and
65+
// returns immediately assuming it can take the mutex lock.
6666
//
67-
// If the *Flock has a shared lock (RLock), this may transparently replace the
68-
// shared lock with an exclusive lock on some UNIX-like operating systems. Be
69-
// careful when using exclusive locks in conjunction with shared locks
70-
// (RLock()), because calling Unlock() may accidentally release the exclusive
71-
// lock that was once a shared lock.
67+
// If the *Flock has a shared lock (RLock),
68+
// this may transparently replace the shared lock with an exclusive lock on some UNIX-like operating systems.
69+
// Be careful when using exclusive locks in conjunction with shared locks (RLock()),
70+
// because calling Unlock() may accidentally release the exclusive lock that was once a shared lock.
7271
func (f *Flock) Lock() error {
7372
return f.lock(&f.l, writeLock)
7473
}
7574

76-
// RLock is a blocking call to try and take a shared file lock. It will wait
77-
// until it is able to obtain the shared file lock. It's recommended that
78-
// TryRLock() be used over this function. This function may block the ability to
79-
// query the current Locked() or RLocked() status due to a RW-mutex lock.
75+
// RLock is a blocking call to try and take a shared file lock.
76+
// It will wait until it is able to obtain the shared file lock.
77+
// It's recommended that TryRLock() be used over this function.
78+
// This function may block the ability to query the current Locked() or RLocked() status due to a RW-mutex lock.
8079
//
81-
// If we are already shared-locked, this function short-circuits and returns
82-
// immediately assuming it can take the mutex lock.
80+
// If we are already shared-locked, this function short-circuits and
81+
// returns immediately assuming it can take the mutex lock.
8382
func (f *Flock) RLock() error {
8483
return f.lock(&f.r, readLock)
8584
}
@@ -110,10 +109,10 @@ func (f *Flock) lock(locked *bool, flag lockType) error {
110109
}
111110

112111
func (f *Flock) doLock(cmd cmdType, lt lockType, blocking bool) (bool, error) {
113-
// POSIX locks apply per inode and process, and the lock for an inode is
114-
// released when *any* descriptor for that inode is closed. So we need to
115-
// synchronize access to each inode internally, and must serialize lock and
116-
// unlock calls that refer to the same inode through different descriptors.
112+
// POSIX locks apply per inode and process,
113+
// and the lock for an inode is released when *any* descriptor for that inode is closed.
114+
// So we need to synchronize access to each inode internally,
115+
// and must serialize lock and unlock calls that refer to the same inode through different descriptors.
117116
fi, err := f.fh.Stat()
118117
if err != nil {
119118
return false, err
@@ -179,8 +178,8 @@ func (f *Flock) Unlock() error {
179178
f.m.Lock()
180179
defer f.m.Unlock()
181180

182-
// if we aren't locked or if the lockfile instance is nil
183-
// just return a nil error because we are unlocked
181+
// If we aren't locked or if the lockfile instance is nil
182+
// just return a nil error because we are unlocked.
184183
if (!f.l && !f.r) || f.fh == nil {
185184
return nil
186185
}
@@ -236,26 +235,28 @@ func (f *Flock) doUnlock() (err error) {
236235
return err
237236
}
238237

239-
// TryLock is the preferred function for taking an exclusive file lock. This
240-
// function takes an RW-mutex lock before it tries to lock the file, so there is
241-
// the possibility that this function may block for a short time if another
242-
// goroutine is trying to take any action.
238+
// TryLock is the preferred function for taking an exclusive file lock.
239+
// This function takes an RW-mutex lock before it tries to lock the file,
240+
// so there is the possibility that this function may block for a short time
241+
// if another goroutine is trying to take any action.
243242
//
244-
// The actual file lock is non-blocking. If we are unable to get the exclusive
245-
// file lock, the function will return false instead of waiting for the lock. If
246-
// we get the lock, we also set the *Flock instance as being exclusive-locked.
243+
// The actual file lock is non-blocking.
244+
// If we are unable to get the exclusive file lock,
245+
// the function will return false instead of waiting for the lock.
246+
// If we get the lock, we also set the *Flock instance as being exclusive-locked.
247247
func (f *Flock) TryLock() (bool, error) {
248248
return f.try(&f.l, writeLock)
249249
}
250250

251-
// TryRLock is the preferred function for taking a shared file lock. This
252-
// function takes an RW-mutex lock before it tries to lock the file, so there is
253-
// the possibility that this function may block for a short time if another
254-
// goroutine is trying to take any action.
251+
// TryRLock is the preferred function for taking a shared file lock.
252+
// This function takes an RW-mutex lock before it tries to lock the file,
253+
// so there is the possibility that this function may block for a short time
254+
// if another goroutine is trying to take any action.
255255
//
256-
// The actual file lock is non-blocking. If we are unable to get the shared file
257-
// lock, the function will return false instead of waiting for the lock. If we
258-
// get the lock, we also set the *Flock instance as being share-locked.
256+
// The actual file lock is non-blocking.
257+
// If we are unable to get the shared file lock,
258+
// the function will return false instead of waiting for the lock.
259+
// If we get the lock, we also set the *Flock instance as being share-locked.
259260
func (f *Flock) TryRLock() (bool, error) {
260261
return f.try(&f.r, readLock)
261262
}

flock_winapi.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ const (
2424
winLockfileSharedLock = 0x00000000
2525
)
2626

27-
// Use of 0x00000000 for the shared lock is a guess based on some the MS Windows
28-
// `LockFileEX` docs, which document the `LOCKFILE_EXCLUSIVE_LOCK` flag as:
27+
// Use of 0x00000000 for the shared lock is a guess based on some the MS Windows `LockFileEX` docs,
28+
// which document the `LOCKFILE_EXCLUSIVE_LOCK` flag as:
2929
//
30-
// > The function requests an exclusive lock. Otherwise, it requests a shared
31-
// > lock.
30+
// > The function requests an exclusive lock. Otherwise, it requests a shared lock.
3231
//
3332
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx
3433

0 commit comments

Comments
 (0)