Skip to content

Commit

Permalink
bug: fix the wrong default behavior of TCPNoDelay on client side (#619)
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Jun 25, 2024
1 parent 5205c5f commit 66c2259
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
3 changes: 1 addition & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ func runClient(t *testing.T, network, addr string, et, reuseport, multicore, asy
clientEV := &clientEvents{tester: t, packetLen: streamLen, svr: ts}
ts.client, err = NewClient(
clientEV,
WithTCPNoDelay(TCPNoDelay),
WithLockOSThread(true),
WithTicker(true),
)
Expand All @@ -456,7 +457,6 @@ func runClient(t *testing.T, network, addr string, et, reuseport, multicore, asy
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute*1),
WithTCPNoDelay(TCPDelay),
WithLoadBalancing(lb))
assert.NoError(t, err)
}
Expand Down Expand Up @@ -657,7 +657,6 @@ func TestClientReadOnEOF(t *testing.T) {
cli, err := NewClient(ev,
WithSocketRecvBuffer(4*1024),
WithSocketSendBuffer(4*1024),
WithTCPNoDelay(TCPDelay),
WithTCPKeepAlive(time.Minute),
WithLogger(zap.NewExample().Sugar()),
WithReadBufferCap(32*1024),
Expand Down
4 changes: 2 additions & 2 deletions client_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,8 @@ func (cli *Client) EnrollContext(c net.Conn, ctx interface{}) (Conn, error) {
ua.Name = c.RemoteAddr().String() + "." + strconv.Itoa(dupFD)
gc = newTCPConn(dupFD, cli.el, sockAddr, c.LocalAddr(), c.RemoteAddr())
case *net.TCPConn:
if cli.opts.TCPNoDelay == TCPDelay {
if err = socket.SetNoDelay(dupFD, 0); err != nil {
if cli.opts.TCPNoDelay == TCPNoDelay {
if err = socket.SetNoDelay(dupFD, 1); err != nil {
return nil, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ func runServer(t *testing.T, addrs []string, et, reuseport, multicore, async, wr
WithReusePort(reuseport),
WithTicker(true),
WithTCPKeepAlive(time.Minute),
WithTCPNoDelay(TCPDelay),
WithTCPNoDelay(TCPNoDelay),
WithLoadBalancing(lb))
} else {
err = Run(ts,
Expand Down
7 changes: 5 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,12 @@ type Options struct {

// TCPNoDelay controls whether the operating system should delay
// packet transmission in hopes of sending fewer packets (Nagle's algorithm).
// When this option is assign to TCPNoDelay, TCP_NODELAY socket option will
// be turned on, on the contrary, if it is assigned to TCPDelay, the socket
// option will be turned off.
//
// The default is true (no delay), meaning that data is sent
// as soon as possible after a write operation.
// The default is TCPNoDelay, meaning that TCP_NODELAY is turned on and data
// will not be buffered but sent as soon as possible after a write operation.
TCPNoDelay TCPSocketOpt

// SocketRecvBuffer sets the maximum socket receive buffer in bytes.
Expand Down

0 comments on commit 66c2259

Please sign in to comment.