Skip to content

Commit 1227fa7

Browse files
committed
Skip sleeping if blocked.
1 parent e455b5f commit 1227fa7

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

conn.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ type Conn struct {
3636
rollback func()
3737
arena arena
3838

39-
kickoff time.Time
39+
busy1st time.Time
40+
busylst time.Time
4041
handle uint32
4142
}
4243

@@ -394,12 +395,15 @@ func timeoutCallback(ctx context.Context, mod api.Module, count, tmout int32) (r
394395
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.interrupt.Err() == nil {
395396
switch {
396397
case count == 0:
397-
c.kickoff = time.Now()
398-
case time.Since(c.kickoff) >= time.Duration(tmout)*time.Millisecond:
398+
c.busy1st = time.Now()
399+
case time.Since(c.busy1st) >= time.Duration(tmout)*time.Millisecond:
399400
return 0
400401
}
401-
const sleepIncrement = 4*1024*1024 - 1 // power of two, ~4ms
402-
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
402+
if time.Since(c.busylst) < time.Millisecond {
403+
const sleepIncrement = 2*1024*1024 - 1 // power of two, ~2ms
404+
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
405+
}
406+
c.busylst = time.Now()
403407
return 1
404408
}
405409
return 0

vfs/os_linux.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package vfs
44

55
import (
6-
"math/rand"
76
"os"
87
"time"
98

@@ -38,23 +37,10 @@ func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, d
3837
}
3938
var err error
4039
switch {
41-
case timeout == 0:
42-
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
4340
case timeout < 0:
4441
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLKW, &lock)
4542
default:
46-
before := time.Now()
47-
for {
48-
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
49-
if errno, _ := err.(unix.Errno); errno != unix.EAGAIN {
50-
break
51-
}
52-
if time.Since(before) > timeout {
53-
break
54-
}
55-
const sleepIncrement = 1024*1024 - 1 // power of two, ~1ms
56-
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
57-
}
43+
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
5844
}
5945
return osLockErrorCode(err, def)
6046
}

0 commit comments

Comments
 (0)