Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
opt: pass the ECONNRESET error to gnet.OnClose() when the peer closes…
Browse files Browse the repository at this point in the history
… connection

Fixes panjf2000#256
panjf2000 committed Jan 7, 2022
1 parent a2444bd commit 0b43fc8
Showing 3 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions connection_bsd.go
Original file line number Diff line number Diff line change
@@ -17,12 +17,16 @@

package gnet

import "github.com/panjf2000/gnet/v2/internal/netpoll"
import (
"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
)

func (c *conn) handleEvents(_ int, filter int16) (err error) {
switch filter {
case netpoll.EVFilterSock:
err = c.loop.closeConn(c, nil)
err = c.loop.closeConn(c, unix.ECONNRESET)
case netpoll.EVFilterWrite:
if !c.outboundBuffer.IsEmpty() {
err = c.loop.write(c)
5 changes: 4 additions & 1 deletion eventloop.go
Original file line number Diff line number Diff line change
@@ -114,12 +114,15 @@ func (el *eventloop) open(c *conn) error {

func (el *eventloop) read(c *conn) error {
n, err := unix.Read(c.fd, el.buffer)
if n == 0 || err != nil {
if err != nil {
if err == unix.EAGAIN {
return nil
}
return el.closeConn(c, os.NewSyscallError("read", err))
}
if n == 0 {
return el.closeConn(c, os.NewSyscallError("read", unix.ECONNRESET))
}

c.buffer = el.buffer[:n]
action := el.eventHandler.OnTraffic(c)
6 changes: 4 additions & 2 deletions reactor_default_bsd.go
Original file line number Diff line number Diff line change
@@ -21,6 +21,8 @@ package gnet
import (
"runtime"

"golang.org/x/sys/unix"

"github.com/panjf2000/gnet/v2/internal/netpoll"
"github.com/panjf2000/gnet/v2/pkg/errors"
)
@@ -56,7 +58,7 @@ func (el *eventloop) activateSubReactor(lockOSThread bool) {
if c, ack := el.connections[fd]; ack {
switch filter {
case netpoll.EVFilterSock:
err = el.closeConn(c, nil)
err = el.closeConn(c, unix.ECONNRESET)
case netpoll.EVFilterWrite:
if !c.outboundBuffer.IsEmpty() {
err = el.write(c)
@@ -90,7 +92,7 @@ func (el *eventloop) run(lockOSThread bool) {
if c, ack := el.connections[fd]; ack {
switch filter {
case netpoll.EVFilterSock:
err = el.closeConn(c, nil)
err = el.closeConn(c, unix.ECONNRESET)
case netpoll.EVFilterWrite:
if !c.outboundBuffer.IsEmpty() {
err = el.write(c)

0 comments on commit 0b43fc8

Please sign in to comment.