修复内存泄漏的问题

This commit is contained in:
haidy 2019-03-07 21:45:38 +08:00
parent 512e601cc9
commit fb756e03de
5 changed files with 50 additions and 10 deletions

View File

@ -145,7 +145,11 @@ func (ctl *Control) HandleNewProxyResp(inMsg *msg.NewProxyResp) {
func (ctl *Control) Close() error { func (ctl *Control) Close() error {
if ctl.conn != nil { if ctl.conn != nil {
ctl.conn.Close() _ = ctl.conn.Close()
ctl.conn = nil
ctl.msgHandlerShutdown = nil
ctl.readerShutdown = nil
ctl.session = nil
log.Info("conn closed") log.Info("conn closed")
} }
if ctl.pm != nil { if ctl.pm != nil {

View File

@ -239,9 +239,10 @@ func (svr *Service) ReloadConf(pxyCfgs map[string]config.ProxyConf, visitorCfgs
func (svr *Service) Close() { func (svr *Service) Close() {
atomic.StoreUint32(&svr.exit, 1) atomic.StoreUint32(&svr.exit, 1)
if svr.ctl != nil { if svr.ctl != nil {
svr.ctl.Close() _ = svr.ctl.Close()
} }
svr.closedCh <- true svr.closedCh <- true
svr.ctl = nil
} }
func (svr *Service) IsClosed() bool { func (svr *Service) IsClosed() bool {

View File

@ -38,6 +38,7 @@ type PortManager struct {
bindAddr string bindAddr string
netType string netType string
mu sync.Mutex mu sync.Mutex
stop chan bool
} }
func NewPortManager(netType string, bindAddr string, allowPorts map[int]struct{}) *PortManager { func NewPortManager(netType string, bindAddr string, allowPorts map[int]struct{}) *PortManager {
@ -47,6 +48,7 @@ func NewPortManager(netType string, bindAddr string, allowPorts map[int]struct{}
freePorts: make(map[int]struct{}), freePorts: make(map[int]struct{}),
bindAddr: bindAddr, bindAddr: bindAddr,
netType: netType, netType: netType,
stop: make(chan bool),
} }
if len(allowPorts) > 0 { if len(allowPorts) > 0 {
for port, _ := range allowPorts { for port, _ := range allowPorts {
@ -176,8 +178,11 @@ func (pm *PortManager) Release(port int) {
// Release reserved port if it isn't used in last 24 hours. // Release reserved port if it isn't used in last 24 hours.
func (pm *PortManager) cleanReservedPortsWorker() { func (pm *PortManager) cleanReservedPortsWorker() {
ticket := time.NewTicker(CleanReservedPortsInterval)
FOR:
for { for {
time.Sleep(CleanReservedPortsInterval) select {
case <-ticket.C:
pm.mu.Lock() pm.mu.Lock()
for name, ctx := range pm.reservedPorts { for name, ctx := range pm.reservedPorts {
if ctx.Closed && time.Since(ctx.UpdateTime) > MaxPortReservedDuration { if ctx.Closed && time.Since(ctx.UpdateTime) > MaxPortReservedDuration {
@ -185,5 +190,17 @@ func (pm *PortManager) cleanReservedPortsWorker() {
} }
} }
pm.mu.Unlock() pm.mu.Unlock()
case s := <-pm.stop:
if s {
ticket.Stop()
close(pm.stop)
break FOR
} }
} }
}
}
// Stop Stop
func (pm *PortManager) Stop() {
pm.stop <- true
}

View File

@ -250,14 +250,24 @@ func (svr *Service) Run() {
// Stop 停止服务 // Stop 停止服务
func (svr *Service) Stop() error { func (svr *Service) Stop() error {
err := svr.muxer.Close() err := svr.muxer.Close()
<-svr.closedCh if svr.listener != nil {
_ = svr.listener.Close()
}
if svr.websocketListener != nil {
_ = svr.websocketListener.Close()
}
if svr.kcpListener != nil {
_ = svr.kcpListener.Close()
}
close(svr.closedCh)
svr.Closed = true svr.Closed = true
svr.rc.TcpPortManager.Stop()
svr.rc.UdpPortManager.Stop()
return err return err
} }
func (svr *Service) HandleListener(l frpNet.Listener) { func (svr *Service) HandleListener(l frpNet.Listener) {
defer func() { defer func() {
close(svr.closedCh)
log.Info("Frps is Closed") log.Info("Frps is Closed")
}() }()
// Listen for incoming connections from client. // Listen for incoming connections from client.

View File

@ -76,6 +76,14 @@ func (p *WebsocketListener) Accept() (Conn, error) {
} }
func (p *WebsocketListener) Close() error { func (p *WebsocketListener) Close() error {
if p.accept != nil {
close(p.accept)
}
if p.ln != nil {
if err := p.ln.Close(); err != nil {
return err
}
}
return p.server.Close() return p.server.Close()
} }