mirror of
https://github.com/fatedier/frp.git
synced 2026-03-08 02:49:10 +08:00
server/control: deduplicate close-proxy logic and UserInfo construction (#5218)
Extract closeProxy() helper to eliminate duplicated 4-step cleanup sequence (Close, PxyManager.Del, metrics, plugin notify) between worker() and CloseProxy(). Extract loginUserInfo() helper to eliminate 4 repeated plugin.UserInfo constructions using LoginMsg fields. Optimize worker() to snapshot and clear the proxies map under lock, then perform cleanup outside the lock to reduce lock hold time.
This commit is contained in:
@@ -303,6 +303,30 @@ func (ctl *Control) WaitClosed() {
|
|||||||
<-ctl.doneCh
|
<-ctl.doneCh
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctl *Control) loginUserInfo() plugin.UserInfo {
|
||||||
|
return plugin.UserInfo{
|
||||||
|
User: ctl.sessionCtx.LoginMsg.User,
|
||||||
|
Metas: ctl.sessionCtx.LoginMsg.Metas,
|
||||||
|
RunID: ctl.sessionCtx.LoginMsg.RunID,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ctl *Control) closeProxy(pxy proxy.Proxy) {
|
||||||
|
pxy.Close()
|
||||||
|
ctl.sessionCtx.PxyManager.Del(pxy.GetName())
|
||||||
|
metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type)
|
||||||
|
|
||||||
|
notifyContent := &plugin.CloseProxyContent{
|
||||||
|
User: ctl.loginUserInfo(),
|
||||||
|
CloseProxy: msg.CloseProxy{
|
||||||
|
ProxyName: pxy.GetName(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
_ = ctl.sessionCtx.PluginManager.CloseProxy(notifyContent)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
func (ctl *Control) worker() {
|
func (ctl *Control) worker() {
|
||||||
xl := ctl.xl
|
xl := ctl.xl
|
||||||
|
|
||||||
@@ -313,31 +337,16 @@ func (ctl *Control) worker() {
|
|||||||
ctl.sessionCtx.Conn.Close()
|
ctl.sessionCtx.Conn.Close()
|
||||||
|
|
||||||
ctl.mu.Lock()
|
ctl.mu.Lock()
|
||||||
defer ctl.mu.Unlock()
|
|
||||||
|
|
||||||
close(ctl.workConnCh)
|
close(ctl.workConnCh)
|
||||||
for workConn := range ctl.workConnCh {
|
for workConn := range ctl.workConnCh {
|
||||||
workConn.Close()
|
workConn.Close()
|
||||||
}
|
}
|
||||||
|
proxies := ctl.proxies
|
||||||
|
ctl.proxies = make(map[string]proxy.Proxy)
|
||||||
|
ctl.mu.Unlock()
|
||||||
|
|
||||||
for _, pxy := range ctl.proxies {
|
for _, pxy := range proxies {
|
||||||
pxy.Close()
|
ctl.closeProxy(pxy)
|
||||||
ctl.sessionCtx.PxyManager.Del(pxy.GetName())
|
|
||||||
metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type)
|
|
||||||
|
|
||||||
notifyContent := &plugin.CloseProxyContent{
|
|
||||||
User: plugin.UserInfo{
|
|
||||||
User: ctl.sessionCtx.LoginMsg.User,
|
|
||||||
Metas: ctl.sessionCtx.LoginMsg.Metas,
|
|
||||||
RunID: ctl.sessionCtx.LoginMsg.RunID,
|
|
||||||
},
|
|
||||||
CloseProxy: msg.CloseProxy{
|
|
||||||
ProxyName: pxy.GetName(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
_ = ctl.sessionCtx.PluginManager.CloseProxy(notifyContent)
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
metrics.Server.CloseClient()
|
metrics.Server.CloseClient()
|
||||||
@@ -360,11 +369,7 @@ func (ctl *Control) handleNewProxy(m msg.Message) {
|
|||||||
inMsg := m.(*msg.NewProxy)
|
inMsg := m.(*msg.NewProxy)
|
||||||
|
|
||||||
content := &plugin.NewProxyContent{
|
content := &plugin.NewProxyContent{
|
||||||
User: plugin.UserInfo{
|
User: ctl.loginUserInfo(),
|
||||||
User: ctl.sessionCtx.LoginMsg.User,
|
|
||||||
Metas: ctl.sessionCtx.LoginMsg.Metas,
|
|
||||||
RunID: ctl.sessionCtx.LoginMsg.RunID,
|
|
||||||
},
|
|
||||||
NewProxy: *inMsg,
|
NewProxy: *inMsg,
|
||||||
}
|
}
|
||||||
var remoteAddr string
|
var remoteAddr string
|
||||||
@@ -399,11 +404,7 @@ func (ctl *Control) handlePing(m msg.Message) {
|
|||||||
inMsg := m.(*msg.Ping)
|
inMsg := m.(*msg.Ping)
|
||||||
|
|
||||||
content := &plugin.PingContent{
|
content := &plugin.PingContent{
|
||||||
User: plugin.UserInfo{
|
User: ctl.loginUserInfo(),
|
||||||
User: ctl.sessionCtx.LoginMsg.User,
|
|
||||||
Metas: ctl.sessionCtx.LoginMsg.Metas,
|
|
||||||
RunID: ctl.sessionCtx.LoginMsg.RunID,
|
|
||||||
},
|
|
||||||
Ping: *inMsg,
|
Ping: *inMsg,
|
||||||
}
|
}
|
||||||
retContent, err := ctl.sessionCtx.PluginManager.Ping(content)
|
retContent, err := ctl.sessionCtx.PluginManager.Ping(content)
|
||||||
@@ -533,25 +534,9 @@ func (ctl *Control) CloseProxy(closeMsg *msg.CloseProxy) (err error) {
|
|||||||
if ctl.sessionCtx.ServerCfg.MaxPortsPerClient > 0 {
|
if ctl.sessionCtx.ServerCfg.MaxPortsPerClient > 0 {
|
||||||
ctl.portsUsedNum -= pxy.GetUsedPortsNum()
|
ctl.portsUsedNum -= pxy.GetUsedPortsNum()
|
||||||
}
|
}
|
||||||
pxy.Close()
|
|
||||||
ctl.sessionCtx.PxyManager.Del(pxy.GetName())
|
|
||||||
delete(ctl.proxies, closeMsg.ProxyName)
|
delete(ctl.proxies, closeMsg.ProxyName)
|
||||||
ctl.mu.Unlock()
|
ctl.mu.Unlock()
|
||||||
|
|
||||||
metrics.Server.CloseProxy(pxy.GetName(), pxy.GetConfigurer().GetBaseConfig().Type)
|
ctl.closeProxy(pxy)
|
||||||
|
|
||||||
notifyContent := &plugin.CloseProxyContent{
|
|
||||||
User: plugin.UserInfo{
|
|
||||||
User: ctl.sessionCtx.LoginMsg.User,
|
|
||||||
Metas: ctl.sessionCtx.LoginMsg.Metas,
|
|
||||||
RunID: ctl.sessionCtx.LoginMsg.RunID,
|
|
||||||
},
|
|
||||||
CloseProxy: msg.CloseProxy{
|
|
||||||
ProxyName: pxy.GetName(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
_ = ctl.sessionCtx.PluginManager.CloseProxy(notifyContent)
|
|
||||||
}()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user