Skip to content

Commit

Permalink
Skip sleeping if blocked.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Dec 13, 2024
1 parent e455b5f commit 1227fa7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
14 changes: 9 additions & 5 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type Conn struct {
rollback func()
arena arena

kickoff time.Time
busy1st time.Time
busylst time.Time
handle uint32
}

Expand Down Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions vfs/os_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package vfs

import (
"math/rand"
"os"
"time"

Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1227fa7

Please sign in to comment.