Skip to content

Commit

Permalink
Revert "opt: improve the event-driven logic"
Browse files Browse the repository at this point in the history
This reverts commit bc75be6.
  • Loading branch information
panjf2000 committed Jul 12, 2021
1 parent bc75be6 commit 4c40e5c
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 19 deletions.
9 changes: 2 additions & 7 deletions connection_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func (c *conn) read() ([]byte, error) {
return c.codec.Decode(c)
}

func (c *conn) write(buf []byte, writeable bool) (err error) {
func (c *conn) write(buf []byte) (err error) {
var outFrame []byte
if outFrame, err = c.codec.Encode(c, buf); err != nil {
return
Expand All @@ -120,11 +120,6 @@ func (c *conn) write(buf []byte, writeable bool) (err error) {
_, _ = c.outboundBuffer.Write(outFrame)
return
}
if !writeable {
_, _ = c.outboundBuffer.Write(outFrame)
err = c.loop.poller.ModReadWrite(c.fd)
return
}

var n int
if n, err = unix.Write(c.fd, outFrame); err != nil {
Expand All @@ -148,7 +143,7 @@ func (c *conn) asyncWrite(itf interface{}) error {
if !c.opened {
return nil
}
return c.write(itf.([]byte), true)
return c.write(itf.([]byte))
}

func (c *conn) sendTo(buf []byte) error {
Expand Down
9 changes: 3 additions & 6 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,7 @@ func (el *eventloop) loopOpen(c *conn) error {
return el.handleAction(c, action)
}

func (el *eventloop) loopRead(c *conn, writeable bool) error {
if writeable && !c.outboundBuffer.IsEmpty() {
return nil
}
func (el *eventloop) loopRead(c *conn) error {
n, err := unix.Read(c.fd, el.buffer)
if n == 0 || err != nil {
if err == unix.EAGAIN {
Expand All @@ -173,7 +170,7 @@ func (el *eventloop) loopRead(c *conn, writeable bool) error {
// Encode data and try to write it back to the client, this attempt is based on a fact:
// a client socket waits for the response data after sending request data to the server,
// which makes the client socket writable.
if err = c.write(out, writeable); err != nil {
if err = c.write(out); err != nil {
return err
}
}
Expand Down Expand Up @@ -268,7 +265,7 @@ func (el *eventloop) loopWake(c *conn) error {

out, action := el.eventHandler.React(nil, c)
if out != nil {
if err := c.write(out, true); err != nil {
if err := c.write(out); err != nil {
return err
}
}
Expand Down
2 changes: 1 addition & 1 deletion loop_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (el *eventloop) handleEvent(fd int, filter int16) (err error) {
case netpoll.EVFilterWrite:
err = el.loopWrite(c)
case netpoll.EVFilterRead:
err = el.loopRead(c, false)
err = el.loopRead(c)
}
return
}
Expand Down
4 changes: 2 additions & 2 deletions loop_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (el *eventloop) handleEvent(fd int, ev uint32) error {
// resulting in that it won't receive any responses before the server read all data from client,
// in which case if the socket send buffer is full, we need to let it go and continue reading the data
// to prevent blocking forever.
if ev&netpoll.InEvents != 0 {
return el.loopRead(c, ev&netpoll.OutEvents != 0)
if ev&netpoll.InEvents != 0 && (ev&netpoll.OutEvents == 0 || c.outboundBuffer.IsEmpty()) {
return el.loopRead(c)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion reactor_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (svr *server) activateSubReactor(el *eventloop, lockOSThread bool) {
case netpoll.EVFilterWrite:
err = el.loopWrite(c)
case netpoll.EVFilterRead:
err = el.loopRead(c, false)
err = el.loopRead(c)
}
}
return
Expand Down
4 changes: 2 additions & 2 deletions reactor_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (svr *server) activateSubReactor(el *eventloop, lockOSThread bool) {
// resulting in that it won't receive any responses before the server reads all data from client,
// in which case if the server socket send buffer is full, we need to let it go and continue reading
// the data to prevent blocking forever.
if ev&netpoll.InEvents != 0 {
return el.loopRead(c, ev&netpoll.OutEvents != 0)
if ev&netpoll.InEvents != 0 && (ev&netpoll.OutEvents == 0 || c.outboundBuffer.IsEmpty()) {
return el.loopRead(c)
}
}
return nil
Expand Down

0 comments on commit 4c40e5c

Please sign in to comment.