Skip to content

Commit

Permalink
Fix gnet.Conn not compatible with net.Conn on method Close having typ…
Browse files Browse the repository at this point in the history
…e func() error and change Close(AsyncCallback) to CloseWithCallback(AsyncCallback)
  • Loading branch information
0-haha committed Mar 25, 2022
1 parent 86b3499 commit 9654480
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func (cli *Client) Dial(network, address string) (Conn, error) {
}
err = cli.el.poller.UrgentTrigger(cli.el.register, gc)
if err != nil {
gc.Close(nil)
gc.Close()
return nil, err
}
return gc, nil
Expand Down
2 changes: 1 addition & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func startGnetClient(t *testing.T, cli *Client, ev *clientEvents, network, addr
rand.Seed(time.Now().UnixNano())
c, err := cli.Dial(network, addr)
require.NoError(t, err)
defer c.Close(nil)
defer c.Close()
var rspCh chan []byte
if network == "udp" {
rspCh = make(chan []byte, 1)
Expand Down
9 changes: 8 additions & 1 deletion connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ func (c *conn) Wake(callback AsyncCallback) error {
}, nil)
}

func (c *conn) Close(callback AsyncCallback) error {
func (c *conn) CloseWithCallback(callback AsyncCallback) error {
return c.loop.poller.Trigger(func(_ interface{}) (err error) {
err = c.loop.closeConn(c, nil)
if callback != nil {
Expand All @@ -451,3 +451,10 @@ func (c *conn) Close(callback AsyncCallback) error {
return
}, nil)
}

func (c *conn) Close() error {
return c.loop.poller.Trigger(func(_ interface{}) (err error) {
err = c.loop.closeConn(c, nil)
return
}, nil)
}
5 changes: 4 additions & 1 deletion gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ type Conn interface {

// Close closes the current connection, usually you don't need to pass a non-nil callback
// because you should use OnClose() instead, the callback here is only for compatibility.
Close(callback AsyncCallback) (err error)
CloseWithCallback(callback AsyncCallback) (err error)

// Close closes the current connection, implements net.Conn.
Close() (err error)
}

type (
Expand Down
4 changes: 2 additions & 2 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ func (t *testCloseConnectionServer) OnTraffic(c Conn) (action Action) {
_, _ = c.Discard(-1)
go func() {
time.Sleep(time.Second)
_ = c.Close(nil)
_ = c.Close()
}()
return
}
Expand Down Expand Up @@ -983,7 +983,7 @@ func (s *testClosedWakeUpServer) OnTraffic(c Conn) Action {
}

go func() { require.NoError(s.tester, c.Wake(nil)) }()
go func() { require.NoError(s.tester, c.Close(nil)) }()
go func() { require.NoError(s.tester, c.Close()) }()

<-s.clientClosed

Expand Down

0 comments on commit 9654480

Please # to comment.