1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 02:30:55 +00:00
Beego/grace/conn.go

40 lines
515 B
Go
Raw Normal View History

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