Skip to content

Commit

Permalink
health: add more ci cases and fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
fatedier committed Dec 9, 2018
1 parent 08c17c3 commit aea9f9f
Show file tree
Hide file tree
Showing 14 changed files with 408 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ gotest:
go test -v --cover ./utils/...

ci:
go test -count=1 -v ./tests/...
go test -count=1 -p=1 -v ./tests/...

alltest: gotest ci

Expand Down
29 changes: 26 additions & 3 deletions client/proxy_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/fatedier/frp/models/msg"
"github.com/fatedier/frp/utils/log"
frpNet "github.com/fatedier/frp/utils/net"

"github.com/fatedier/golib/errors"
)

const (
Expand Down Expand Up @@ -55,6 +57,7 @@ type ProxyWrapper struct {
lastSendStartMsg time.Time
lastStartErr time.Time
closeCh chan struct{}
healthNotifyCh chan struct{}
mu sync.RWMutex

log.Logger
Expand All @@ -69,9 +72,10 @@ func NewProxyWrapper(cfg config.ProxyConf, eventHandler EventHandler, logPrefix
Status: ProxyStatusNew,
Cfg: cfg,
},
closeCh: make(chan struct{}),
handler: eventHandler,
Logger: log.NewPrefixLogger(logPrefix),
closeCh: make(chan struct{}),
healthNotifyCh: make(chan struct{}),
handler: eventHandler,
Logger: log.NewPrefixLogger(logPrefix),
}
pw.AddLogPrefix(pw.Name)

Expand Down Expand Up @@ -125,6 +129,8 @@ func (pw *ProxyWrapper) Start() {
func (pw *ProxyWrapper) Stop() {
pw.mu.Lock()
defer pw.mu.Unlock()
close(pw.closeCh)
close(pw.healthNotifyCh)
pw.pxy.Close()
if pw.monitor != nil {
pw.monitor.Stop()
Expand All @@ -139,6 +145,10 @@ func (pw *ProxyWrapper) Stop() {
}

func (pw *ProxyWrapper) checkWorker() {
if pw.monitor != nil {
// let monitor do check request first
time.Sleep(500 * time.Millisecond)
}
for {
// check proxy status
now := time.Now()
Expand Down Expand Up @@ -178,17 +188,30 @@ func (pw *ProxyWrapper) checkWorker() {
case <-pw.closeCh:
return
case <-time.After(statusCheckInterval):
case <-pw.healthNotifyCh:
}
}
}

func (pw *ProxyWrapper) statusNormalCallback() {
atomic.StoreUint32(&pw.health, 0)
errors.PanicToError(func() {
select {
case pw.healthNotifyCh <- struct{}{}:
default:
}
})
pw.Info("health check success")
}

func (pw *ProxyWrapper) statusFailedCallback() {
atomic.StoreUint32(&pw.health, 1)
errors.PanicToError(func() {
select {
case pw.healthNotifyCh <- struct{}{}:
default:
}
})
pw.Info("health check failed")
}

Expand Down
5 changes: 5 additions & 0 deletions cmd/frpc/sub/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ func parseClientCommonCfgFromCmd() (err error) {
g.GlbClientCfg.LogLevel = logLevel
g.GlbClientCfg.LogFile = logFile
g.GlbClientCfg.LogMaxDays = int64(logMaxDays)
if logFile == "console" {
g.GlbClientCfg.LogWay = "console"
} else {
g.GlbClientCfg.LogWay = "file"
}
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions cmd/frps/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ var (
dashboardPwd string
assetsDir string
logFile string
logWay string
logLevel string
logMaxDays int64
token string
Expand Down Expand Up @@ -81,7 +80,6 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&dashboardUser, "dashboard_user", "", "admin", "dashboard user")
rootCmd.PersistentFlags().StringVarP(&dashboardPwd, "dashboard_pwd", "", "admin", "dashboard password")
rootCmd.PersistentFlags().StringVarP(&logFile, "log_file", "", "console", "log file")
rootCmd.PersistentFlags().StringVarP(&logWay, "log_way", "", "console", "log way")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log_level", "", "info", "log level")
rootCmd.PersistentFlags().Int64VarP(&logMaxDays, "log_max_days", "", 3, "log_max_days")
rootCmd.PersistentFlags().StringVarP(&token, "token", "t", "", "auth token")
Expand Down Expand Up @@ -175,7 +173,6 @@ func parseServerCommonCfgFromCmd() (err error) {
g.GlbServerCfg.DashboardUser = dashboardUser
g.GlbServerCfg.DashboardPwd = dashboardPwd
g.GlbServerCfg.LogFile = logFile
g.GlbServerCfg.LogWay = logWay
g.GlbServerCfg.LogLevel = logLevel
g.GlbServerCfg.LogMaxDays = logMaxDays
g.GlbServerCfg.Token = token
Expand All @@ -194,6 +191,12 @@ func parseServerCommonCfgFromCmd() (err error) {
}
}
g.GlbServerCfg.MaxPortsPerClient = maxPortsPerClient

if logFile == "console" {
g.GlbClientCfg.LogWay = "console"
} else {
g.GlbClientCfg.LogWay = "file"
}
return
}

Expand Down
2 changes: 2 additions & 0 deletions conf/frpc_full.ini
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ group_key = 123456
# frpc will connect local service's port to detect it's healthy status
health_check_type = tcp
health_check_interval_s = 10
health_check_max_failed = 1
health_check_timeout_s = 3

[ssh_random]
type = tcp
Expand Down
7 changes: 7 additions & 0 deletions models/config/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ func (cfg *BaseProxyConf) UnmarshalFromIni(prefix string, name string, section i
if cfg.HealthCheckType == "tcp" && cfg.Plugin == "" {
cfg.HealthCheckAddr = cfg.LocalIp + fmt.Sprintf(":%d", cfg.LocalPort)
}
if cfg.HealthCheckType == "http" && cfg.Plugin == "" && cfg.HealthCheckUrl != "" {
s := fmt.Sprintf("http://%s:%d", cfg.LocalIp, cfg.LocalPort)
if !strings.HasPrefix(cfg.HealthCheckUrl, "/") {
s += "/"
}
cfg.HealthCheckUrl = s + cfg.HealthCheckUrl
}
return nil
}

Expand Down
Loading

0 comments on commit aea9f9f

Please sign in to comment.