pr review feedback

This commit is contained in:
Quentin McGaw
2026-06-11 14:01:05 +00:00
parent b44c671217
commit 08dfd73367
+16 -7
View File
@@ -17,7 +17,16 @@ func closeFD(fd int) {
} }
func newTCPSockStream(family int) (fd int, err error) { func newTCPSockStream(family int) (fd int, err error) {
return unix.Socket(family, unix.SOCK_STREAM, unix.IPPROTO_TCP) fd, err = unix.Socket(family, unix.SOCK_STREAM, unix.IPPROTO_TCP)
if err != nil {
return 0, err
}
err = unix.SetNonblock(fd, true)
if err != nil {
_ = unix.Close(fd)
return 0, err
}
return fd, nil
} }
func bindFD(fd int, address netip.AddrPort) error { func bindFD(fd int, address netip.AddrPort) error {
@@ -37,16 +46,16 @@ func connectFD(ctx context.Context, fd int, destination netip.AddrPort) error {
for { for {
select { select {
case <-ctx.Done(): case <-ctx.Done():
err = unix.Close(fd)
if err != nil {
return fmt.Errorf("error closing fd: %w (%w)", err, ctx.Err())
}
return ctx.Err() return ctx.Err()
default: default:
bitsIndex := fd / 64 //nolint:mnd
if bitsIndex >= len(unix.FdSet{}.Bits) {
return fmt.Errorf("fd %d exceeds unix.Select FdSet capacity", fd)
}
wset := &unix.FdSet{} wset := &unix.FdSet{}
wset.Bits[fd/64] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd wset.Bits[bitsIndex] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd
eset := &unix.FdSet{} eset := &unix.FdSet{}
eset.Bits[fd/64] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd eset.Bits[bitsIndex] |= 1 << (uint64(fd) % 64) //nolint:gosec,mnd
const selectTimeout = 50 * time.Millisecond const selectTimeout = 50 * time.Millisecond
timeval := unix.NsecToTimeval(int64(selectTimeout)) timeval := unix.NsecToTimeval(int64(selectTimeout))