Skip to content

Commit dc08c5c

Browse files
committed
Fix unix writev
1 parent d199bc5 commit dc08c5c

File tree

2 files changed

+30
-31
lines changed

2 files changed

+30
-31
lines changed

common/bufio/vectorised.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,10 @@ func NewVectorisedWriter(writer io.Writer) N.VectorisedWriter {
2020
}
2121

2222
func CreateVectorisedWriter(writer any) (N.VectorisedWriter, bool) {
23-
var rawConn syscall.RawConn
24-
switch w := writer.(type) {
25-
case N.VectorisedWriter:
26-
return w, true
27-
case syscall.Conn:
28-
var err error
29-
rawConn, err = w.SyscallConn()
30-
if err != nil {
31-
return nil, false
32-
}
33-
case syscall.RawConn:
34-
rawConn = w
35-
default:
36-
return nil, false
37-
}
3823
if runtime.GOOS == "windows" {
3924
switch conn := writer.(type) {
25+
case N.VectorisedWriter:
26+
return conn, true
4027
case *net.TCPConn:
4128
return &NetVectorisedWriterWrapper{conn}, true
4229
case *net.UDPConn:
@@ -45,11 +32,22 @@ func CreateVectorisedWriter(writer any) (N.VectorisedWriter, bool) {
4532
return &NetVectorisedWriterWrapper{conn}, true
4633
case *net.UnixConn:
4734
return &NetVectorisedWriterWrapper{conn}, true
48-
default:
49-
return nil, false
35+
}
36+
} else {
37+
switch conn := writer.(type) {
38+
case N.VectorisedWriter:
39+
return conn, true
40+
case syscall.Conn:
41+
rawConn, err := conn.SyscallConn()
42+
if err != nil {
43+
return nil, false
44+
}
45+
return &SyscallVectorisedWriter{upstream: writer, rawConn: rawConn}, true
46+
case syscall.RawConn:
47+
return &SyscallVectorisedWriter{upstream: writer, rawConn: conn}, true
5048
}
5149
}
52-
return &SyscallVectorisedWriter{upstream: writer, rawConn: rawConn}, false
50+
return nil, false
5351
}
5452

5553
func CreateVectorisedPacketWriter(writer any) (N.VectorisedPacketWriter, bool) {

common/bufio/vectorised_unix.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,35 +27,36 @@ func (w *SyscallVectorisedWriter) WriteVectorised(buffers []*buf.Buffer) error {
2727
for _, buffer := range buffers {
2828
iovecList = append(iovecList, buffer.Iovec(buffer.Len()))
2929
}
30+
if cap(iovecList) > cap(w.iovecList) {
31+
w.iovecList = iovecList[:0]
32+
}
3033
var innerErr unix.Errno
3134
err := w.rawConn.Write(func(fd uintptr) (done bool) {
35+
writeIovecList := iovecList
3236
for {
3337
var r0 uintptr
3438
//nolint:staticcheck
35-
r0, _, innerErr = unix.RawSyscall(unix.SYS_WRITEV, fd, uintptr(unsafe.Pointer(&iovecList[0])), uintptr(len(iovecList)))
39+
r0, _, innerErr = unix.RawSyscall(unix.SYS_WRITEV, fd, uintptr(unsafe.Pointer(&writeIovecList[0])), uintptr(len(writeIovecList)))
3640
writeN := int(r0)
3741
for writeN > 0 {
3842
if buffers[0].Len() > writeN {
3943
buffers[0].Advance(writeN)
40-
iovecList[0] = buffers[0].Iovec(buffers[0].Len())
44+
writeIovecList[0] = buffers[0].Iovec(buffers[0].Len())
4145
break
4246
} else {
4347
writeN -= buffers[0].Len()
4448
buffers[0].Release()
4549
buffers = buffers[1:]
46-
iovecList = iovecList[1:]
50+
writeIovecList = writeIovecList[1:]
4751
}
4852
}
49-
if innerErr == unix.EINTR || (innerErr == 0 && len(iovecList) > 0) {
53+
if innerErr == unix.EINTR || (innerErr == 0 && len(writeIovecList) > 0) {
5054
continue
5155
}
5256
return innerErr != unix.EAGAIN
5357
}
5458
})
5559
common.ClearArray(iovecList)
56-
if cap(iovecList) > cap(w.iovecList) {
57-
w.iovecList = iovecList[:0]
58-
}
5960
if innerErr != 0 {
6061
err = os.NewSyscallError("SYS_WRITEV", innerErr)
6162
}
@@ -70,7 +71,10 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
7071
for _, buffer := range buffers {
7172
iovecList = append(iovecList, buffer.Iovec(buffer.Len()))
7273
}
73-
var innerErr error
74+
if cap(iovecList) > cap(w.iovecList) {
75+
w.iovecList = iovecList[:0]
76+
}
77+
var innerErr unix.Errno
7478
err := w.rawConn.Write(func(fd uintptr) (done bool) {
7579
var msg unix.Msghdr
7680
name, nameLen := ToSockaddr(destination.AddrPort())
@@ -89,11 +93,8 @@ func (w *SyscallVectorisedPacketWriter) WriteVectorisedPacket(buffers []*buf.Buf
8993
}
9094
})
9195
common.ClearArray(iovecList)
92-
if cap(iovecList) > cap(w.iovecList) {
93-
w.iovecList = iovecList[:0]
94-
}
95-
if innerErr != nil {
96-
err = innerErr
96+
if innerErr != 0 {
97+
err = os.NewSyscallError("SYS_SENDMSG", innerErr)
9798
}
9899
return err
99100
}

0 commit comments

Comments
 (0)