2019-03-07 02:07:53 -08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2019-09-18 18:17:32 -07:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-08-09 20:15:25 -07:00
|
|
|
"github.com/astaxie/beego/logs"
|
2019-03-14 23:03:49 -07:00
|
|
|
"github.com/cnlh/nps/lib/conn"
|
2019-03-07 02:07:53 -08:00
|
|
|
"github.com/cnlh/nps/lib/file"
|
|
|
|
"github.com/cnlh/nps/lib/sheap"
|
2019-03-14 23:03:49 -07:00
|
|
|
"github.com/pkg/errors"
|
2019-03-07 02:07:53 -08:00
|
|
|
)
|
|
|
|
|
2019-03-14 23:03:49 -07:00
|
|
|
var isStart bool
|
|
|
|
var serverConn *conn.Conn
|
|
|
|
|
|
|
|
func heathCheck(healths []*file.Health, c *conn.Conn) bool {
|
|
|
|
serverConn = c
|
|
|
|
if isStart {
|
|
|
|
for _, v := range healths {
|
2019-03-07 02:07:53 -08:00
|
|
|
v.HealthMap = make(map[string]int)
|
|
|
|
}
|
2019-03-14 23:03:49 -07:00
|
|
|
return true
|
2019-03-07 02:07:53 -08:00
|
|
|
}
|
2019-03-14 23:03:49 -07:00
|
|
|
isStart = true
|
|
|
|
h := &sheap.IntHeap{}
|
|
|
|
for _, v := range healths {
|
2019-03-07 02:07:53 -08:00
|
|
|
if v.HealthMaxFail > 0 && v.HealthCheckTimeout > 0 && v.HealthCheckInterval > 0 {
|
2019-03-14 23:03:49 -07:00
|
|
|
v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval) * time.Second)
|
2019-03-07 02:07:53 -08:00
|
|
|
heap.Push(h, v.HealthNextTime.Unix())
|
|
|
|
v.HealthMap = make(map[string]int)
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 23:03:49 -07:00
|
|
|
go session(healths, h)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func session(healths []*file.Health, h *sheap.IntHeap) {
|
2019-03-07 02:07:53 -08:00
|
|
|
for {
|
2019-03-14 23:03:49 -07:00
|
|
|
if h.Len() == 0 {
|
|
|
|
logs.Error("health check error")
|
|
|
|
break
|
|
|
|
}
|
2019-03-07 02:07:53 -08:00
|
|
|
rs := heap.Pop(h).(int64) - time.Now().Unix()
|
2019-03-14 23:03:49 -07:00
|
|
|
if rs <= 0 {
|
2019-03-07 02:07:53 -08:00
|
|
|
continue
|
|
|
|
}
|
2019-03-14 23:03:49 -07:00
|
|
|
timer := time.NewTimer(time.Duration(rs) * time.Second)
|
2019-03-07 02:07:53 -08:00
|
|
|
select {
|
|
|
|
case <-timer.C:
|
2019-03-14 23:03:49 -07:00
|
|
|
for _, v := range healths {
|
2019-03-07 02:07:53 -08:00
|
|
|
if v.HealthNextTime.Before(time.Now()) {
|
2019-03-14 23:03:49 -07:00
|
|
|
v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval) * time.Second)
|
2019-03-07 02:07:53 -08:00
|
|
|
//check
|
2019-03-14 23:03:49 -07:00
|
|
|
go check(v)
|
2019-03-07 02:07:53 -08:00
|
|
|
//reset time
|
|
|
|
heap.Push(h, v.HealthNextTime.Unix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:41:57 -07:00
|
|
|
// work when just one port and many target
|
2019-03-14 23:03:49 -07:00
|
|
|
func check(t *file.Health) {
|
|
|
|
arr := strings.Split(t.HealthCheckTarget, ",")
|
|
|
|
var err error
|
|
|
|
var rs *http.Response
|
2019-03-07 02:07:53 -08:00
|
|
|
for _, v := range arr {
|
2019-03-14 23:03:49 -07:00
|
|
|
if t.HealthCheckType == "tcp" {
|
2019-09-18 18:17:32 -07:00
|
|
|
var c net.Conn
|
|
|
|
c, err = net.DialTimeout("tcp", v, time.Duration(t.HealthCheckTimeout)*time.Second)
|
|
|
|
if err == nil {
|
|
|
|
c.Close()
|
|
|
|
}
|
2019-03-14 23:03:49 -07:00
|
|
|
} else {
|
|
|
|
client := &http.Client{}
|
|
|
|
client.Timeout = time.Duration(t.HealthCheckTimeout) * time.Second
|
|
|
|
rs, err = client.Get("http://" + v + t.HttpHealthUrl)
|
|
|
|
if err == nil && rs.StatusCode != 200 {
|
|
|
|
err = errors.New("status code is not match")
|
|
|
|
}
|
2019-03-07 02:07:53 -08:00
|
|
|
}
|
2019-03-25 20:13:07 -07:00
|
|
|
t.Lock()
|
2019-03-14 23:03:49 -07:00
|
|
|
if err != nil {
|
2019-03-07 02:07:53 -08:00
|
|
|
t.HealthMap[v] += 1
|
|
|
|
} else if t.HealthMap[v] >= t.HealthMaxFail {
|
|
|
|
//send recovery add
|
2019-03-14 23:03:49 -07:00
|
|
|
serverConn.SendHealthInfo(v, "1")
|
2019-03-07 02:07:53 -08:00
|
|
|
t.HealthMap[v] = 0
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:41:57 -07:00
|
|
|
if t.HealthMap[v] > 0 && t.HealthMap[v]%t.HealthMaxFail == 0 {
|
2019-03-14 23:03:49 -07:00
|
|
|
//send fail remove
|
|
|
|
serverConn.SendHealthInfo(v, "0")
|
2019-03-07 02:07:53 -08:00
|
|
|
}
|
2019-03-25 20:13:07 -07:00
|
|
|
t.Unlock()
|
2019-03-07 02:07:53 -08:00
|
|
|
}
|
|
|
|
}
|