From 1227fa7a04bec9d3d6b3260f79b7dacf33b35842 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 13 Dec 2024 16:04:37 +0000 Subject: [PATCH] Skip sleeping if blocked. --- conn.go | 14 +++++++++----- vfs/os_linux.go | 16 +--------------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/conn.go b/conn.go index 45c294bb..d1ce3055 100644 --- a/conn.go +++ b/conn.go @@ -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 diff --git a/vfs/os_linux.go b/vfs/os_linux.go index 0db6b276..6199c7b0 100644 --- a/vfs/os_linux.go +++ b/vfs/os_linux.go @@ -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) }