Skip to content

Commit

Permalink
peek sockets instead of selecting
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
  • Loading branch information
evacchi committed Jul 18, 2023
1 parent ffa0af6 commit 5ab89da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 39 deletions.
9 changes: 4 additions & 5 deletions internal/platform/fdset_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (f *FdSet) SetRegular(r WinSockFdSet) {
f.regular = r
}

// Regular returns a WinSockFdSet containing the handles in this FdSet that are pipes.
// Pipes returns a WinSockFdSet containing the handles in this FdSet that are pipes.
func (f *FdSet) Pipes() *WinSockFdSet {
if f == nil {
return nil
Expand Down Expand Up @@ -187,13 +187,12 @@ func (f *WinSockFdSet) Get(index int) syscall.Handle {
return f.handles[index]
}

// isSocket returns true if the given file handle is a pipe.
// isSocket returns true if the given file handle
// is a pipe.
func isSocket(fd syscall.Handle) bool {
// If the call to GetNamedPipeInfo succeeds then
// the handle is a pipe handle, otherwise it is a socket.
r, _, errno := syscall.SyscallN(
procGetNamedPipeInfo.Addr(),
uintptr(unsafe.Pointer(nil)),
uintptr(fd),
uintptr(unsafe.Pointer(nil)),
uintptr(unsafe.Pointer(nil)),
uintptr(unsafe.Pointer(nil)))
Expand Down
39 changes: 5 additions & 34 deletions internal/sysfs/select_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,26 +95,6 @@ func selectAllHandles(ctx context.Context, r, w, e *platform.FdSet, duration *ti
afterCh = after.C
}

// winsock_select is a blocking call. We spin a goroutine
// and write back to a channel the result. We consume
// this result in the for loop together with the polling
// routines.
type selectResult struct {
n int
errno sys.Errno
}

winsockSelectCh := make(chan selectResult, 1)
defer close(winsockSelectCh)

go func() {
res := selectResult{}
res.n, res.errno = winsock_select(rs, ws, es, duration)
winsockSelectCh <- res
}()

nsocks := 0

outer:
for {
select {
Expand All @@ -125,24 +105,15 @@ func selectAllHandles(ctx context.Context, r, w, e *platform.FdSet, duration *ti
case <-tickCh:
rp, errno = peekAllPipes(r.Pipes())
npipes = rp.Count()
if errno != 0 || npipes > 0 {
if errno != 0 {
break outer
}
case res := <-winsockSelectCh:
nsocks = res.n
if res.errno != 0 {

zero := time.Duration(0)
nsocks, errno = winsock_select(rs, ws, es, &zero)
if errno != 0 {
break outer
}
// winsock_select has returned with no result, ignore
// and wait for the other pipes.
if nsocks == 0 {
continue
}
// If select has return successfully we peek for the last time at the other pipes
// to see if data is available and return the sum.
rp, errno = peekAllPipes(r.Pipes())
npipes = rp.Count()
break outer
}
}
}
Expand Down

0 comments on commit 5ab89da

Please sign in to comment.