2015-05-27 23:46:45 +08:00
|
|
|
package grace
|
|
|
|
|
2016-01-04 23:34:35 +08:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2017-06-13 15:19:51 +08:00
|
|
|
"sync"
|
2016-01-04 23:34:35 +08:00
|
|
|
)
|
2015-05-27 23:46:45 +08:00
|
|
|
|
|
|
|
type graceConn struct {
|
|
|
|
net.Conn
|
2015-09-10 16:35:40 +08:00
|
|
|
server *Server
|
2017-07-19 00:52:27 +08:00
|
|
|
m sync.Mutex
|
2017-06-13 15:19:51 +08:00
|
|
|
closed bool
|
2015-05-27 23:46:45 +08:00
|
|
|
}
|
|
|
|
|
2017-06-13 20:15:43 +08:00
|
|
|
func (c *graceConn) Close() (err error) {
|
2016-01-04 23:34:35 +08:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
switch x := r.(type) {
|
|
|
|
case string:
|
|
|
|
err = errors.New(x)
|
|
|
|
case error:
|
|
|
|
err = x
|
|
|
|
default:
|
|
|
|
err = errors.New("Unknown panic")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2017-07-19 00:52:27 +08:00
|
|
|
|
2017-06-13 15:19:51 +08:00
|
|
|
c.m.Lock()
|
2019-01-03 22:44:32 +09:00
|
|
|
defer c.m.Unlock()
|
2017-06-13 15:19:51 +08:00
|
|
|
if c.closed {
|
|
|
|
return
|
|
|
|
}
|
2015-05-27 23:46:45 +08:00
|
|
|
c.server.wg.Done()
|
2017-06-13 15:19:51 +08:00
|
|
|
c.closed = true
|
2015-05-27 23:46:45 +08:00
|
|
|
return c.Conn.Close()
|
2017-06-13 15:34:57 +08:00
|
|
|
}
|