1
0
mirror of https://github.com/astaxie/beego.git synced 2025-01-11 08:57:13 +00:00
Beego/grace/conn.go

39 lines
506 B
Go
Raw Normal View History

2015-05-27 23:46:45 +08:00
package grace
2016-01-04 23:34:35 +08:00
import (
"errors"
"net"
"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
closed bool
2015-05-27 23:46:45 +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
c.m.Lock()
defer c.m.Unlock()
if c.closed {
return
}
2015-05-27 23:46:45 +08:00
c.server.wg.Done()
c.closed = true
2015-05-27 23:46:45 +08:00
return c.Conn.Close()
}