Skip to content

Commit

Permalink
feat: add Options.AsyncWriteNoDelay
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Mar 29, 2024
1 parent e2d2ee3 commit 4989790
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
10 changes: 8 additions & 2 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
15 changes: 15 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
}

0 comments on commit 4989790

Please sign in to comment.