health.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package client
  2. import (
  3. "container/heap"
  4. "github.com/cnlh/nps/lib/config"
  5. "github.com/cnlh/nps/lib/file"
  6. "github.com/cnlh/nps/lib/sheap"
  7. "net"
  8. "net/http"
  9. "strings"
  10. "time"
  11. )
  12. func heathCheck(cnf *config.Config, c net.Conn) {
  13. var hosts []*file.Host
  14. var tunnels []*file.Tunnel
  15. h := &sheap.IntHeap{}
  16. for _, v := range cnf.Hosts {
  17. if v.HealthMaxFail > 0 && v.HealthCheckTimeout > 0 && v.HealthCheckInterval > 0 {
  18. v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval))
  19. heap.Push(h, v.HealthNextTime.Unix())
  20. v.HealthMap = make(map[string]int)
  21. hosts = append(hosts, v)
  22. }
  23. }
  24. for _, v := range cnf.Tasks {
  25. if v.HealthMaxFail > 0 && v.HealthCheckTimeout > 0 && v.HealthCheckInterval > 0 {
  26. v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval))
  27. heap.Push(h, v.HealthNextTime.Unix())
  28. v.HealthMap = make(map[string]int)
  29. tunnels = append(tunnels, v)
  30. }
  31. }
  32. if len(hosts) == 0 && len(tunnels) == 0 {
  33. return
  34. }
  35. for {
  36. rs := heap.Pop(h).(int64) - time.Now().Unix()
  37. if rs < 0 {
  38. continue
  39. }
  40. timer := time.NewTicker(time.Duration(rs))
  41. select {
  42. case <-timer.C:
  43. for _, v := range hosts {
  44. if v.HealthNextTime.Before(time.Now()) {
  45. v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval))
  46. //check
  47. go checkHttp(v, c)
  48. //reset time
  49. heap.Push(h, v.HealthNextTime.Unix())
  50. }
  51. }
  52. for _, v := range tunnels {
  53. if v.HealthNextTime.Before(time.Now()) {
  54. v.HealthNextTime = time.Now().Add(time.Duration(v.HealthCheckInterval))
  55. //check
  56. go checkTcp(v, c)
  57. //reset time
  58. heap.Push(h, v.HealthNextTime.Unix())
  59. }
  60. }
  61. }
  62. }
  63. }
  64. func checkTcp(t *file.Tunnel, c net.Conn) {
  65. arr := strings.Split(t.Target, "\n")
  66. for _, v := range arr {
  67. if _, err := net.DialTimeout("tcp", v, time.Duration(t.HealthCheckTimeout)); err != nil {
  68. t.HealthMap[v] += 1
  69. }
  70. if t.HealthMap[v] > t.HealthMaxFail {
  71. t.HealthMap[v] += 1
  72. if t.HealthMap[v] == t.HealthMaxFail {
  73. //send fail remove
  74. ch <- file.NewHealthInfo("tcp", v, true)
  75. }
  76. } else if t.HealthMap[v] >= t.HealthMaxFail {
  77. //send recovery add
  78. ch <- file.NewHealthInfo("tcp", v, false)
  79. t.HealthMap[v] = 0
  80. }
  81. }
  82. }
  83. func checkHttp(h *file.Host, ch chan *file.HealthInfo) {
  84. arr := strings.Split(h.Target, "\n")
  85. client := &http.Client{}
  86. client.Timeout = time.Duration(h.HealthCheckTimeout) * time.Second
  87. for _, v := range arr {
  88. if _, err := client.Get(v + h.HttpHealthUrl); err != nil {
  89. h.HealthMap[v] += 1
  90. if h.HealthMap[v] == h.HealthMaxFail {
  91. //send fail remove
  92. ch <- file.NewHealthInfo("http", v, true)
  93. }
  94. } else if h.HealthMap[v] >= h.HealthMaxFail {
  95. //send recovery add
  96. h.HealthMap[v] = 0
  97. ch <- file.NewHealthInfo("http", v, false)
  98. }
  99. }
  100. }