frpc: udpate proxies check and start logic

This commit is contained in:
fatedier
2018-01-26 00:23:48 +08:00
parent ce8fde793c
commit 637ddbce1f
3 changed files with 35 additions and 29 deletions

View File

@@ -69,14 +69,10 @@ func NewProxyWrapper(cfg config.ProxyConf) *ProxyWrapper {
}
}
func (pw *ProxyWrapper) IsRunning() bool {
func (pw *ProxyWrapper) GetStatusStr() string {
pw.mu.RLock()
defer pw.mu.RUnlock()
if pw.Status == ProxyStatusRunning {
return true
} else {
return false
}
return pw.Status
}
func (pw *ProxyWrapper) GetStatus() *ProxyStatus {
@@ -210,7 +206,8 @@ func (pm *ProxyManager) CloseProxies() {
}
}
func (pm *ProxyManager) CheckAndStartProxy() {
// pxyStatus: check and start proxies in which status
func (pm *ProxyManager) CheckAndStartProxy(pxyStatus []string) {
pm.mu.RLock()
defer pm.mu.RUnlock()
if pm.closed {
@@ -219,13 +216,17 @@ func (pm *ProxyManager) CheckAndStartProxy() {
}
for _, pxy := range pm.proxies {
if !pxy.IsRunning() {
var newProxyMsg msg.NewProxy
pxy.Cfg.UnMarshalToMsg(&newProxyMsg)
err := pm.sendMsg(&newProxyMsg)
if err != nil {
pm.Warn("[%s] proxy send NewProxy message error")
return
status := pxy.GetStatusStr()
for _, s := range pxyStatus {
if status == s {
var newProxyMsg msg.NewProxy
pxy.Cfg.UnMarshalToMsg(&newProxyMsg)
err := pm.sendMsg(&newProxyMsg)
if err != nil {
pm.Warn("[%s] proxy send NewProxy message error")
return
}
break
}
}
}
@@ -245,9 +246,14 @@ func (pm *ProxyManager) CheckAndStartProxy() {
}
}
func (pm *ProxyManager) Reload(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.ProxyConf) error {
func (pm *ProxyManager) Reload(pxyCfgs map[string]config.ProxyConf, visitorCfgs map[string]config.ProxyConf, startNow bool) error {
pm.mu.Lock()
defer pm.mu.Unlock()
defer func() {
pm.mu.Unlock()
if startNow {
go pm.CheckAndStartProxy([]string{ProxyStatusNew})
}
}()
if pm.closed {
err := fmt.Errorf("Reload error: ProxyManager is closed now")
pm.Warn(err.Error())