Skip to content

Commit

Permalink
update: use WSAPoll if only sockets
Browse files Browse the repository at this point in the history
Calling WSAPoll with a timeout is more economical, and we can safely do that if only sockets are being poll'd.

Signed-off-by: Gaukas Wang <i@gaukas.wang>
  • Loading branch information
gaukas committed Jan 18, 2024
1 parent f3783b2 commit 7574f58
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions internal/sysfs/poll_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ func _poll(fds []pollFd, timeoutMillis int32) (n int, errno sys.Errno) {
return -1, errno
}

npipes, nsockets, errno := peekAll(pipes, sockets)
if errno != 0 {
return -1, errno
}
count := nregular + npipes + nsockets
if count > 0 {
return count, 0
}

// At this point, we know there are:
// - no regular files in the list
// - no pipes or sockets are ready
//
// If there are only sockets, we can invoke wsaPoll with the given timeout.
if len(pipes) == 0 {
return wsaPoll(sockets, int(timeoutMillis))
}

// Otherwise, we need to check both pipes and sockets, and cannot use wsaPoll.
// We use a ticker to trigger a check periodically, and a timer to expire after
// the given timeout.

// Ticker that emits at every pollInterval.
tick := time.NewTicker(pollInterval)
tickCh := tick.C
Expand All @@ -81,15 +103,6 @@ func _poll(fds []pollFd, timeoutMillis int32) (n int, errno sys.Errno) {
afterCh = after.C
}

npipes, nsockets, errno := peekAll(pipes, sockets)
if errno != 0 {
return -1, errno
}
count := nregular + npipes + nsockets
if count > 0 {
return count, 0
}

for {
select {
case <-afterCh:
Expand Down

0 comments on commit 7574f58

Please sign in to comment.