5
0
mirror of https://github.com/cwinfo/yggdrasil-go.git synced 2024-11-26 20:01:38 +00:00

don't block forever in Write if the session is cancelled, cleanup Conn.Read slightly

This commit is contained in:
Arceliar 2019-08-04 02:08:47 -05:00
parent 144c823bee
commit 6da5802ae5

View File

@ -154,7 +154,6 @@ func (c *Conn) Read(b []byte) (int, error) {
sinfo := c.session sinfo := c.session
cancel := c.getDeadlineCancellation(&c.readDeadline) cancel := c.getDeadlineCancellation(&c.readDeadline)
defer cancel.Cancel(nil) defer cancel.Cancel(nil)
for {
// Wait for some traffic to come through from the session // Wait for some traffic to come through from the session
select { select {
case <-cancel.Finished(): case <-cancel.Finished():
@ -178,7 +177,6 @@ func (c *Conn) Read(b []byte) (int, error) {
return n, err return n, err
} }
} }
}
func (c *Conn) Write(b []byte) (bytesWritten int, err error) { func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
sinfo := c.session sinfo := c.session
@ -206,7 +204,17 @@ func (c *Conn) Write(b []byte) (bytesWritten int, err error) {
sinfo.doFunc(sessionFunc) sinfo.doFunc(sessionFunc)
if written > 0 { if written > 0 {
bs := append(util.GetBytes(), b...) bs := append(util.GetBytes(), b...)
sinfo.send <- bs cancel := c.getDeadlineCancellation(&c.writeDeadline)
defer cancel.Cancel(nil)
select {
case <-cancel.Finished():
if cancel.Error() == util.CancellationTimeoutError {
return 0, ConnError{errors.New("write timeout"), true, false, false, 0}
} else {
return 0, ConnError{errors.New("session closed"), false, false, true, 0}
}
case sinfo.send <- bs:
}
} }
return written, err return written, err
} }