health.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2018 fatedier, fatedier@gmail.com
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package health
  15. import (
  16. "context"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "net"
  21. "net/http"
  22. "strings"
  23. "time"
  24. v1 "github.com/fatedier/frp/pkg/config/v1"
  25. "github.com/fatedier/frp/pkg/util/xlog"
  26. )
  27. var ErrHealthCheckType = errors.New("error health check type")
  28. type Monitor struct {
  29. checkType string
  30. interval time.Duration
  31. timeout time.Duration
  32. maxFailedTimes int
  33. // For tcp
  34. addr string
  35. // For http
  36. url string
  37. failedTimes uint64
  38. statusOK bool
  39. statusNormalFn func()
  40. statusFailedFn func()
  41. ctx context.Context
  42. cancel context.CancelFunc
  43. }
  44. func NewMonitor(ctx context.Context, cfg v1.HealthCheckConfig, addr string,
  45. statusNormalFn func(), statusFailedFn func(),
  46. ) *Monitor {
  47. if cfg.IntervalSeconds <= 0 {
  48. cfg.IntervalSeconds = 10
  49. }
  50. if cfg.TimeoutSeconds <= 0 {
  51. cfg.TimeoutSeconds = 3
  52. }
  53. if cfg.MaxFailed <= 0 {
  54. cfg.MaxFailed = 1
  55. }
  56. newctx, cancel := context.WithCancel(ctx)
  57. var url string
  58. if cfg.Type == "http" && cfg.Path != "" {
  59. s := "http://" + addr
  60. if !strings.HasPrefix(cfg.Path, "/") {
  61. s += "/"
  62. }
  63. url = s + cfg.Path
  64. }
  65. return &Monitor{
  66. checkType: cfg.Type,
  67. interval: time.Duration(cfg.IntervalSeconds) * time.Second,
  68. timeout: time.Duration(cfg.TimeoutSeconds) * time.Second,
  69. maxFailedTimes: cfg.MaxFailed,
  70. addr: addr,
  71. url: url,
  72. statusOK: false,
  73. statusNormalFn: statusNormalFn,
  74. statusFailedFn: statusFailedFn,
  75. ctx: newctx,
  76. cancel: cancel,
  77. }
  78. }
  79. func (monitor *Monitor) Start() {
  80. go monitor.checkWorker()
  81. }
  82. func (monitor *Monitor) Stop() {
  83. monitor.cancel()
  84. }
  85. func (monitor *Monitor) checkWorker() {
  86. xl := xlog.FromContextSafe(monitor.ctx)
  87. for {
  88. doCtx, cancel := context.WithDeadline(monitor.ctx, time.Now().Add(monitor.timeout))
  89. err := monitor.doCheck(doCtx)
  90. // check if this monitor has been closed
  91. select {
  92. case <-monitor.ctx.Done():
  93. cancel()
  94. return
  95. default:
  96. cancel()
  97. }
  98. if err == nil {
  99. xl.Tracef("do one health check success")
  100. if !monitor.statusOK && monitor.statusNormalFn != nil {
  101. xl.Infof("health check status change to success")
  102. monitor.statusOK = true
  103. monitor.statusNormalFn()
  104. }
  105. } else {
  106. xl.Warnf("do one health check failed: %v", err)
  107. monitor.failedTimes++
  108. if monitor.statusOK && int(monitor.failedTimes) >= monitor.maxFailedTimes && monitor.statusFailedFn != nil {
  109. xl.Warnf("health check status change to failed")
  110. monitor.statusOK = false
  111. monitor.statusFailedFn()
  112. }
  113. }
  114. time.Sleep(monitor.interval)
  115. }
  116. }
  117. func (monitor *Monitor) doCheck(ctx context.Context) error {
  118. switch monitor.checkType {
  119. case "tcp":
  120. return monitor.doTCPCheck(ctx)
  121. case "http":
  122. return monitor.doHTTPCheck(ctx)
  123. default:
  124. return ErrHealthCheckType
  125. }
  126. }
  127. func (monitor *Monitor) doTCPCheck(ctx context.Context) error {
  128. // if tcp address is not specified, always return nil
  129. if monitor.addr == "" {
  130. return nil
  131. }
  132. var d net.Dialer
  133. conn, err := d.DialContext(ctx, "tcp", monitor.addr)
  134. if err != nil {
  135. return err
  136. }
  137. conn.Close()
  138. return nil
  139. }
  140. func (monitor *Monitor) doHTTPCheck(ctx context.Context) error {
  141. req, err := http.NewRequestWithContext(ctx, "GET", monitor.url, nil)
  142. if err != nil {
  143. return err
  144. }
  145. resp, err := http.DefaultClient.Do(req)
  146. if err != nil {
  147. return err
  148. }
  149. defer resp.Body.Close()
  150. _, _ = io.Copy(io.Discard, resp.Body)
  151. if resp.StatusCode/100 != 2 {
  152. return fmt.Errorf("do http health check, StatusCode is [%d] not 2xx", resp.StatusCode)
  153. }
  154. return nil
  155. }