Skip to content

Commit

Permalink
fix: eliminate duplicate calls to loopCloseConn
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed Aug 21, 2020
1 parent 43f93ca commit a72f5fd
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions eventloop_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ type eventloop struct {
ln *listener // listener
idx int // loop index in the server loops list
svr *server // server in loop
packet []byte // read packet buffer
poller *netpoll.Poller // epoll or kqueue
packet []byte // read packet buffer
connCount int32 // number of active connections in event-loop
connections map[int]*conn // loop connections fd -> conn
eventHandler EventHandler // user eventHandler
Expand Down Expand Up @@ -165,7 +165,7 @@ func (el *eventloop) loopWrite(c *conn) error {
}
c.outboundBuffer.Shift(n)

if len(head) == n && tail != nil {
if n == len(head) && tail != nil {
n, err = unix.Write(c.fd, tail)
if err != nil {
if err == unix.EAGAIN {
Expand All @@ -189,8 +189,15 @@ func (el *eventloop) loopCloseConn(c *conn, err error) error {
return nil
}

if !c.outboundBuffer.IsEmpty() && err == nil {
_ = el.loopWrite(c)
if !c.outboundBuffer.IsEmpty() {
el.eventHandler.PreWrite()

head, tail := c.outboundBuffer.LazyReadAll()
if n, err := unix.Write(c.fd, head); err == nil {
if n == len(head) && tail != nil {
_, _ = unix.Write(c.fd, tail)
}
}
}

if err0, err1 := el.poller.Delete(c.fd), unix.Close(c.fd); err0 == nil && err1 == nil {
Expand Down

0 comments on commit a72f5fd

Please sign in to comment.