Skip sleeping if blocked.

This commit is contained in:
Nuno Cruces
2024-12-13 16:04:37 +00:00
parent e455b5f729
commit 1227fa7a04
2 changed files with 10 additions and 20 deletions

14
conn.go
View File

@@ -36,7 +36,8 @@ type Conn struct {
rollback func()
arena arena
kickoff time.Time
busy1st time.Time
busylst time.Time
handle uint32
}
@@ -394,12 +395,15 @@ func timeoutCallback(ctx context.Context, mod api.Module, count, tmout int32) (r
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.interrupt.Err() == nil {
switch {
case count == 0:
c.kickoff = time.Now()
case time.Since(c.kickoff) >= time.Duration(tmout)*time.Millisecond:
c.busy1st = time.Now()
case time.Since(c.busy1st) >= time.Duration(tmout)*time.Millisecond:
return 0
}
const sleepIncrement = 4*1024*1024 - 1 // power of two, ~4ms
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
if time.Since(c.busylst) < time.Millisecond {
const sleepIncrement = 2*1024*1024 - 1 // power of two, ~2ms
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
}
c.busylst = time.Now()
return 1
}
return 0

View File

@@ -3,7 +3,6 @@
package vfs
import (
"math/rand"
"os"
"time"
@@ -38,23 +37,10 @@ func osLock(file *os.File, typ int16, start, len int64, timeout time.Duration, d
}
var err error
switch {
case timeout == 0:
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
case timeout < 0:
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLKW, &lock)
default:
before := time.Now()
for {
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
if errno, _ := err.(unix.Errno); errno != unix.EAGAIN {
break
}
if time.Since(before) > timeout {
break
}
const sleepIncrement = 1024*1024 - 1 // power of two, ~1ms
time.Sleep(time.Duration(rand.Int63() & sleepIncrement))
}
err = unix.FcntlFlock(file.Fd(), unix.F_OFD_SETLK, &lock)
}
return osLockErrorCode(err, def)
}