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

40 lines
511 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
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")
}
}
}()
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()
}