frp/client/health/health.go

172 lines
3.9 KiB
Go
Raw Normal View History

// Copyright 2018 fatedier, fatedier@gmail.com
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-12-09 15:06:22 +01:00
package health
import (
2018-07-15 19:21:29 +02:00
"context"
2018-12-07 10:05:36 +01:00
"errors"
"fmt"
"io"
"io/ioutil"
2018-07-15 19:21:29 +02:00
"net"
"net/http"
"time"
2018-12-07 10:05:36 +01:00
2019-10-12 14:13:12 +02:00
"github.com/fatedier/frp/utils/xlog"
2018-12-07 10:05:36 +01:00
)
var (
ErrHealthCheckType = errors.New("error health check type")
)
type HealthCheckMonitor struct {
2018-07-15 19:21:29 +02:00
checkType string
interval time.Duration
timeout time.Duration
maxFailedTimes int
// For tcp
addr string
// For http
url string
failedTimes uint64
statusOK bool
statusNormalFn func()
statusFailedFn func()
ctx context.Context
cancel context.CancelFunc
}
2019-10-12 14:13:12 +02:00
func NewHealthCheckMonitor(ctx context.Context, checkType string,
intervalS int, timeoutS int, maxFailedTimes int,
addr string, url string,
2018-07-15 19:21:29 +02:00
statusNormalFn func(), statusFailedFn func()) *HealthCheckMonitor {
if intervalS <= 0 {
intervalS = 10
}
if timeoutS <= 0 {
timeoutS = 3
}
if maxFailedTimes <= 0 {
maxFailedTimes = 1
}
2019-10-12 14:13:12 +02:00
newctx, cancel := context.WithCancel(ctx)
return &HealthCheckMonitor{
2018-07-15 19:21:29 +02:00
checkType: checkType,
interval: time.Duration(intervalS) * time.Second,
timeout: time.Duration(timeoutS) * time.Second,
maxFailedTimes: maxFailedTimes,
addr: addr,
url: url,
statusOK: false,
statusNormalFn: statusNormalFn,
statusFailedFn: statusFailedFn,
2019-10-12 14:13:12 +02:00
ctx: newctx,
2018-07-15 19:21:29 +02:00
cancel: cancel,
}
}
func (monitor *HealthCheckMonitor) Start() {
2018-07-15 19:21:29 +02:00
go monitor.checkWorker()
}
func (monitor *HealthCheckMonitor) Stop() {
monitor.cancel()
}
func (monitor *HealthCheckMonitor) checkWorker() {
2019-10-12 14:13:12 +02:00
xl := xlog.FromContextSafe(monitor.ctx)
2018-07-15 19:21:29 +02:00
for {
2019-08-09 06:47:27 +02:00
doCtx, cancel := context.WithDeadline(monitor.ctx, time.Now().Add(monitor.timeout))
err := monitor.doCheck(doCtx)
2018-07-15 19:21:29 +02:00
// check if this monitor has been closed
select {
2019-08-09 06:47:27 +02:00
case <-monitor.ctx.Done():
2018-07-15 19:21:29 +02:00
cancel()
return
default:
cancel()
}
2018-12-07 10:05:36 +01:00
if err == nil {
2019-10-12 14:13:12 +02:00
xl.Trace("do one health check success")
2018-07-15 19:21:29 +02:00
if !monitor.statusOK && monitor.statusNormalFn != nil {
2019-10-12 14:13:12 +02:00
xl.Info("health check status change to success")
2018-07-15 19:21:29 +02:00
monitor.statusOK = true
monitor.statusNormalFn()
}
} else {
2019-10-12 14:13:12 +02:00
xl.Warn("do one health check failed: %v", err)
2018-07-15 19:21:29 +02:00
monitor.failedTimes++
if monitor.statusOK && int(monitor.failedTimes) >= monitor.maxFailedTimes && monitor.statusFailedFn != nil {
2019-10-12 14:13:12 +02:00
xl.Warn("health check status change to failed")
2018-07-15 19:21:29 +02:00
monitor.statusOK = false
monitor.statusFailedFn()
}
}
time.Sleep(monitor.interval)
}
}
2018-12-07 10:05:36 +01:00
func (monitor *HealthCheckMonitor) doCheck(ctx context.Context) error {
2018-07-15 19:21:29 +02:00
switch monitor.checkType {
case "tcp":
return monitor.doTcpCheck(ctx)
case "http":
return monitor.doHttpCheck(ctx)
default:
2018-12-07 10:05:36 +01:00
return ErrHealthCheckType
2018-07-15 19:21:29 +02:00
}
}
2018-12-07 10:05:36 +01:00
func (monitor *HealthCheckMonitor) doTcpCheck(ctx context.Context) error {
// if tcp address is not specified, always return nil
if monitor.addr == "" {
return nil
}
2018-07-15 19:21:29 +02:00
var d net.Dialer
conn, err := d.DialContext(ctx, "tcp", monitor.addr)
if err != nil {
2018-12-07 10:05:36 +01:00
return err
2018-07-15 19:21:29 +02:00
}
conn.Close()
2018-12-07 10:05:36 +01:00
return nil
2018-07-15 19:21:29 +02:00
}
2018-12-07 10:05:36 +01:00
func (monitor *HealthCheckMonitor) doHttpCheck(ctx context.Context) error {
2018-07-15 19:21:29 +02:00
req, err := http.NewRequest("GET", monitor.url, nil)
if err != nil {
2018-12-07 10:05:36 +01:00
return err
2018-07-15 19:21:29 +02:00
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
2018-12-07 10:05:36 +01:00
return err
2018-07-15 19:21:29 +02:00
}
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
2018-07-15 19:21:29 +02:00
if resp.StatusCode/100 != 2 {
2018-12-07 10:05:36 +01:00
return fmt.Errorf("do http health check, StatusCode is [%d] not 2xx", resp.StatusCode)
2018-07-15 19:21:29 +02:00
}
2018-12-07 10:05:36 +01:00
return nil
}