Skip to content

Commit

Permalink
feat: add DisableTCPNoDelay to turn on Nagle's algorithm
Browse files Browse the repository at this point in the history
Signed-off-by: Rueian <[email protected]>
  • Loading branch information
rueian committed Oct 20, 2024
1 parent 343326f commit 912f223
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion pipe.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type pipe struct {
waits int32
recvs int32
r2ps bool // identify this pipe is used for resp2 pubsub or not
noNoDelay bool
}

type pipeFn func(connFn func() (net.Conn, error), option *ClientOption) (p *pipe, err error)
Expand All @@ -98,6 +99,7 @@ func _newPipe(connFn func() (net.Conn, error), option *ClientOption, r2ps, nobg
timeout: option.ConnWriteTimeout,
pinggap: option.Dialer.KeepAlive,
maxFlushDelay: option.MaxFlushDelay,
noNoDelay: option.DisableTCPNoDelay,

r2ps: r2ps,
}
Expand Down Expand Up @@ -321,7 +323,7 @@ func (p *pipe) _exit(err error) {

func (p *pipe) _background() {
p.conn.SetDeadline(time.Time{})
if conn, ok := p.conn.(*net.TCPConn); ok {
if conn, ok := p.conn.(*net.TCPConn); ok && p.noNoDelay {
conn.SetNoDelay(false)
}
go func() {
Expand Down
5 changes: 5 additions & 0 deletions rueidis.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ type ClientOption struct {
// produce notable CPU usage reduction under load. Ref: https://github.com/redis/rueidis/issues/156
MaxFlushDelay time.Duration

// DisableTCPNoDelay turns on Nagle's algorithm in pipelining mode by using conn.SetNoDelay(false).
// Turning this on can result in lower p99 latencies and lower CPU usages if all your requests are small and rapid.
// But if you have large requests, performance might degrade. Ref: https://github.com/redis/rueidis/pull/650
DisableTCPNoDelay bool

// ShuffleInit is a handy flag that shuffles the InitAddress after passing to the NewClient() if it is true
ShuffleInit bool
// ClientNoTouch controls whether commands alter LRU/LFU stats
Expand Down

0 comments on commit 912f223

Please sign in to comment.