From 912f223fb76ef1c980f00a9f279a40bedf5846f7 Mon Sep 17 00:00:00 2001 From: Rueian Date: Sat, 19 Oct 2024 22:21:37 -0700 Subject: [PATCH] feat: add DisableTCPNoDelay to turn on Nagle's algorithm Signed-off-by: Rueian --- pipe.go | 4 +++- rueidis.go | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pipe.go b/pipe.go index a147a008..e54d94d6 100644 --- a/pipe.go +++ b/pipe.go @@ -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) @@ -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, } @@ -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() { diff --git a/rueidis.go b/rueidis.go index 0fb8f62c..3a747d79 100644 --- a/rueidis.go +++ b/rueidis.go @@ -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