2015-05-27 15:46:45 +00:00
|
|
|
package grace
|
|
|
|
|
2016-01-04 15:34:35 +00:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net"
|
2017-06-13 07:19:51 +00:00
|
|
|
"sync"
|
2016-01-04 15:34:35 +00:00
|
|
|
)
|
2015-05-27 15:46:45 +00:00
|
|
|
|
|
|
|
type graceConn struct {
|
|
|
|
net.Conn
|
2015-09-10 08:35:40 +00:00
|
|
|
server *Server
|
2017-06-13 07:19:51 +00:00
|
|
|
m sync.Mutex
|
|
|
|
closed bool
|
2015-05-27 15:46:45 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 12:15:43 +00:00
|
|
|
func (c *graceConn) Close() (err error) {
|
2016-01-04 15:34:35 +00: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-06-13 07:19:51 +00:00
|
|
|
|
|
|
|
c.m.Lock()
|
|
|
|
if c.closed {
|
|
|
|
c.m.Unlock()
|
|
|
|
return
|
|
|
|
}
|
2015-05-27 15:46:45 +00:00
|
|
|
c.server.wg.Done()
|
2017-06-13 07:19:51 +00:00
|
|
|
c.closed = true
|
|
|
|
c.m.Unlock()
|
2015-05-27 15:46:45 +00:00
|
|
|
return c.Conn.Close()
|
2017-06-13 07:34:57 +00:00
|
|
|
}
|