Skip to content

Commit

Permalink
feat: support TCP_NODELAY (#39)
Browse files Browse the repository at this point in the history
* feat: support TCP_NODELAY

* feat: default tcp_nodelay = true

* feat: hide intertal interface

* chore: fix tests
  • Loading branch information
joway authored and Hchenn committed Sep 16, 2021
1 parent f343971 commit 8224c3e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
5 changes: 5 additions & 0 deletions connection_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ func (c *connection) init(conn Conn, prepare OnPrepare) (err error) {
c.inputBarrier, c.outputBarrier = barrierPool.Get().(*barrier), barrierPool.Get().(*barrier)
c.setFinalizer()

// enable TCP_NODELAY by default
switch c.network {
case "tcp", "tcp4", "tcp6":
setTCPNoDelay(c.fd, true)
}
// check zero-copy
if setZeroCopy(c.fd) == nil && setBlockZeroCopySend(c.fd, defaultZeroCopyTimeoutSec, 0) == nil {
c.supportZeroCopy = true
Expand Down
14 changes: 14 additions & 0 deletions connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,17 @@ func TestConnectionLargeMemory(t *testing.T) {
panic(fmt.Sprintf("alloc[%d] out of memory %d", alloc, limit))
}
}

// TestSetTCPNoDelay is used to verify the connection initialization set the TCP_NODELAY correctly
func TestSetTCPNoDelay(t *testing.T) {
fd, err := sysSocket(syscall.AF_INET, syscall.SOCK_STREAM, 0)
conn := &connection{}
conn.init(&netFD{network: "tcp", fd: fd}, nil)

n, _ := syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY)
MustTrue(t, n > 0)
err = setTCPNoDelay(fd, false)
MustNil(t, err)
n, _ = syscall.GetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY)
MustTrue(t, n == 0)
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/bytedance/gopkg v0.0.0-20210507130052-24980f04837a h1:wjM2Z8ZV1OjQMyVSoXIDqjWK5BEuRHT+S6kLH1BVR5c=
github.com/bytedance/gopkg v0.0.0-20210507130052-24980f04837a/go.mod h1:birsdqRCbwnckJbdAvcSao+AzOyibVEoWB55MjpYpB8=
github.com/bytedance/gopkg v0.0.0-20210705062217-74c74ebadcae h1:ERLYTdHnm2E8jwpprhHPvBhbPBaxnwl62tb3lR8Nd+k=
github.com/bytedance/gopkg v0.0.0-20210705062217-74c74ebadcae/go.mod h1:birsdqRCbwnckJbdAvcSao+AzOyibVEoWB55MjpYpB8=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
Expand Down
13 changes: 13 additions & 0 deletions sys_exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func GetSysFdPairs() (r, w int) {
return fds[0], fds[1]
}

// setTCPNoDelay set the TCP_NODELAY flag on socket
func setTCPNoDelay(fd int, b bool) (err error) {
return syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, boolint(b))
}

// Wrapper around the socket system call that marks the returned file
// descriptor as nonblocking and close-on-exec.
func sysSocket(family, sotype, proto int) (int, error) {
Expand Down Expand Up @@ -101,3 +106,11 @@ func iovecs(bs [][]byte, ivs []syscall.Iovec) (iovLen int) {
}
return iovLen
}

// Boolean to int.
func boolint(b bool) int {
if b {
return 1
}
return 0
}
8 changes: 0 additions & 8 deletions sys_sockopt_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,3 @@ func setDefaultSockopts(s, family, sotype int, ipv6only bool) error {
// Allow broadcast.
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(s, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1))
}

// Boolean to int.
func boolint(b bool) int {
if b {
return 1
}
return 0
}

0 comments on commit 8224c3e

Please sign in to comment.