github.com/iceber/iouring-go@v0.0.0-20230403020409-002cfd2e2a90/timeout.go (about) 1 //go:build linux 2 // +build linux 3 4 package iouring 5 6 import ( 7 "time" 8 "unsafe" 9 10 "golang.org/x/sys/unix" 11 12 iouring_syscall "github.com/iceber/iouring-go/syscall" 13 ) 14 15 func (prepReq PrepRequest) WithTimeout(timeout time.Duration) []PrepRequest { 16 linkRequest := func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) { 17 prepReq(sqe, userData) 18 sqe.SetFlags(iouring_syscall.IOSQE_FLAGS_IO_LINK) 19 } 20 return []PrepRequest{linkRequest, linkTimeout(timeout)} 21 } 22 23 func Timeout(t time.Duration) PrepRequest { 24 timespec := unix.NsecToTimespec(t.Nanoseconds()) 25 26 return func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) { 27 userData.hold(×pec) 28 userData.request.resolver = timeoutResolver 29 30 sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, uint64(uintptr(unsafe.Pointer(×pec))), 1, 0) 31 } 32 } 33 34 func TimeoutWithTime(t time.Time) (PrepRequest, error) { 35 timespec, err := unix.TimeToTimespec(t) 36 if err != nil { 37 return nil, err 38 } 39 40 return func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) { 41 userData.hold(×pec) 42 userData.request.resolver = timeoutResolver 43 44 sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, uint64(uintptr(unsafe.Pointer(×pec))), 1, 0) 45 sqe.SetOpFlags(iouring_syscall.IORING_TIMEOUT_ABS) 46 }, nil 47 } 48 49 func CountCompletionEvent(n uint64) PrepRequest { 50 return func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) { 51 userData.request.resolver = timeoutResolver 52 53 sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, 0, 0, n) 54 } 55 } 56 57 func RemoveTimeout(id uint64) PrepRequest { 58 return func(sqe iouring_syscall.SubmissionQueueEntry, userData *UserData) { 59 userData.request.resolver = removeTimeoutResolver 60 61 sqe.PrepOperation(iouring_syscall.IORING_OP_TIMEOUT, -1, id, 0, 0) 62 } 63 }