Skip to content

Commit

Permalink
opt: redefine AsyncCallback, pass in the error message
Browse files Browse the repository at this point in the history
Note: this is a breaking change, the existing codebase using AsyncCallback must be updated accordingly.

Fixes #398
  • Loading branch information
panjf2000 committed Oct 18, 2022
1 parent 3705a09 commit 691e077
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
10 changes: 5 additions & 5 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (c *conn) asyncWrite(itf interface{}) (err error) {
hook := itf.(*asyncWriteHook)
_, err = c.write(hook.data)
if hook.callback != nil {
_ = hook.callback(c)
_ = hook.callback(c, err)
}
return
}
Expand All @@ -237,7 +237,7 @@ func (c *conn) asyncWritev(itf interface{}) (err error) {
hook := itf.(*asyncWritevHook)
_, err = c.writev(hook.data)
if hook.callback != nil {
_ = hook.callback(c)
_ = hook.callback(c, err)
}
return
}
Expand Down Expand Up @@ -438,7 +438,7 @@ func (c *conn) AsyncWrite(buf []byte, callback AsyncCallback) error {
if c.isDatagram {
defer func() {
if callback != nil {
_ = callback(nil)
_ = callback(nil, nil)
}
}()
return c.sendTo(buf)
Expand All @@ -457,7 +457,7 @@ func (c *conn) Wake(callback AsyncCallback) error {
return c.loop.poller.UrgentTrigger(func(_ interface{}) (err error) {
err = c.loop.wake(c)
if callback != nil {
_ = callback(c)
_ = callback(c, err)
}
return
}, nil)
Expand All @@ -467,7 +467,7 @@ 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 {
_ = callback(c)
_ = callback(c, err)
}
return
}, nil)
Expand Down
17 changes: 9 additions & 8 deletions gnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type Writer interface {
// AsyncCallback is a callback which will be invoked after the asynchronous functions has finished executing.
//
// Note that the parameter gnet.Conn is already released under UDP protocol, thus it's not allowed to be accessed.
type AsyncCallback func(c Conn) error
type AsyncCallback func(c Conn, err error) error

// Socket is a set of functions which manipulate the underlying file descriptor of a connection.
type Socket interface {
Expand Down Expand Up @@ -318,13 +318,14 @@ var MaxStreamBufferCap = 64 * 1024 // 64KB
// Address should use a scheme prefix and be formatted
// like `tcp://192.168.0.10:9851` or `unix://socket`.
// Valid network schemes:
// tcp - bind to both IPv4 and IPv6
// tcp4 - IPv4
// tcp6 - IPv6
// udp - bind to both IPv4 and IPv6
// udp4 - IPv4
// udp6 - IPv6
// unix - Unix Domain Socket
//
// tcp - bind to both IPv4 and IPv6
// tcp4 - IPv4
// tcp6 - IPv6
// udp - bind to both IPv4 and IPv6
// udp4 - IPv4
// udp6 - IPv6
// unix - Unix Domain Socket
//
// The "tcp" network scheme is assumed when one is not specified.
func Run(eventHandler EventHandler, protoAddr string, opts ...Option) (err error) {
Expand Down
12 changes: 6 additions & 6 deletions gnet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ func (s *testServer) OnTraffic(c Conn) (action Action) {
bs := make([][]byte, 2)
bs[0] = buf.B[:mid]
bs[1] = buf.B[mid:]
_ = c.AsyncWritev(bs, func(c Conn) error {
logging.Debugf("conn=%s done writev", c.RemoteAddr().String())
_ = c.AsyncWritev(bs, func(c Conn, err error) error {
logging.Debugf("conn=%s done writev: %v", c.RemoteAddr().String(), err)
return nil
})
} else {
_ = c.AsyncWrite(buf.Bytes(), func(c Conn) error {
logging.Debugf("conn=%s done write", c.RemoteAddr().String())
_ = c.AsyncWrite(buf.Bytes(), func(c Conn, err error) error {
logging.Debugf("conn=%s done write: %v", c.RemoteAddr().String(), err)
return nil
})
}
Expand Down Expand Up @@ -514,8 +514,8 @@ func (t *testWakeConnServer) OnTick() (delay time.Duration, action Action) {
return
}
t.c = <-t.conn
_ = t.c.Wake(func(c Conn) error {
logging.Debugf("conn=%s done wake", c.RemoteAddr().String())
_ = t.c.Wake(func(c Conn, err error) error {
logging.Debugf("conn=%s done wake: %v", c.RemoteAddr().String(), err)
return nil
})
delay = time.Millisecond * 100
Expand Down

0 comments on commit 691e077

Please sign in to comment.