From 498979072c9f2a6206d4176a4e7b3c682407d428 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Fri, 29 Mar 2024 22:52:10 +0800 Subject: [PATCH] feat: add Options.AsyncWriteNoDelay --- connection_unix.go | 10 ++++++++-- gnet_test.go | 1 + options.go | 15 +++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/connection_unix.go b/connection_unix.go index 3269847d7..818237cac 100644 --- a/connection_unix.go +++ b/connection_unix.go @@ -442,14 +442,20 @@ func (c *conn) AsyncWrite(buf []byte, callback AsyncCallback) error { } return err } - return c.loop.poller.UrgentTrigger(c.asyncWrite, &asyncWriteHook{callback, buf}) + if c.loop.engine.opts.AsyncWriteNoDelay { + return c.loop.poller.UrgentTrigger(c.asyncWrite, &asyncWriteHook{callback, buf}) + } + return c.loop.poller.Trigger(c.asyncWrite, &asyncWriteHook{callback, buf}) } func (c *conn) AsyncWritev(bs [][]byte, callback AsyncCallback) error { if c.isDatagram { return errorx.ErrUnsupportedOp } - return c.loop.poller.UrgentTrigger(c.asyncWritev, &asyncWritevHook{callback, bs}) + if c.loop.engine.opts.AsyncWriteNoDelay { + return c.loop.poller.UrgentTrigger(c.asyncWritev, &asyncWritevHook{callback, bs}) + } + return c.loop.poller.Trigger(c.asyncWritev, &asyncWritevHook{callback, bs}) } func (c *conn) Wake(callback AsyncCallback) error { diff --git a/gnet_test.go b/gnet_test.go index e6150023d..90a4f2304 100644 --- a/gnet_test.go +++ b/gnet_test.go @@ -397,6 +397,7 @@ func testServe(t *testing.T, network, addr string, reuseport, reuseaddr, multico WithTicker(true), WithTCPKeepAlive(time.Minute*1), WithTCPNoDelay(TCPDelay), + WithAsyncWriteNoDelay(reuseport), WithLoadBalancing(lb)) assert.NoError(t, err) } diff --git a/options.go b/options.go index fb39e7fbf..bb2b3b5a5 100644 --- a/options.go +++ b/options.go @@ -122,6 +122,12 @@ type Options struct { // Logger is the customized logger for logging info, if it is not set, // then gnet will use the default logger powered by go.uber.org/zap. Logger logging.Logger + + // AsyncWriteNoDelay controls whether to execute all the asynchronous tasks of Conn.AsyncWrite/AsyncWritev + // at once without deferring them to the next event-loop iteration. Don't enable this option unless you know + // what you're doing. + // Check out #423 for more details. + AsyncWriteNoDelay bool } // WithOptions sets up all options. @@ -249,3 +255,12 @@ func WithMulticastInterfaceIndex(idx int) Option { opts.MulticastInterfaceIndex = idx } } + +// WithAsyncWriteNoDelay sets up the AsyncWriteNoDelay option. +// Don't enable this option unless you know what you're doing. +// Check out #423 for more details. +func WithAsyncWriteNoDelay(noDelay bool) Option { + return func(opts *Options) { + opts.AsyncWriteNoDelay = noDelay + } +}