mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-11 21:49:13 +00:00
Skip sleeping if blocked.
This commit is contained in:
14
conn.go
14
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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user