1
0

health.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 client
  15. import (
  16. "context"
  17. "net"
  18. "net/http"
  19. "time"
  20. )
  21. type HealthCheckMonitor struct {
  22. checkType string
  23. interval time.Duration
  24. timeout time.Duration
  25. maxFailedTimes int
  26. // For tcp
  27. addr string
  28. // For http
  29. url string
  30. failedTimes uint64
  31. statusOK bool
  32. statusNormalFn func()
  33. statusFailedFn func()
  34. ctx context.Context
  35. cancel context.CancelFunc
  36. }
  37. func NewHealthCheckMonitor(checkType string, intervalS int, timeoutS int, maxFailedTimes int, addr string, url string,
  38. statusNormalFn func(), statusFailedFn func()) *HealthCheckMonitor {
  39. if intervalS <= 0 {
  40. intervalS = 10
  41. }
  42. if timeoutS <= 0 {
  43. timeoutS = 3
  44. }
  45. if maxFailedTimes <= 0 {
  46. maxFailedTimes = 1
  47. }
  48. ctx, cancel := context.WithCancel(context.Background())
  49. return &HealthCheckMonitor{
  50. checkType: checkType,
  51. interval: time.Duration(intervalS) * time.Second,
  52. timeout: time.Duration(timeoutS) * time.Second,
  53. maxFailedTimes: maxFailedTimes,
  54. addr: addr,
  55. url: url,
  56. statusOK: false,
  57. statusNormalFn: statusNormalFn,
  58. statusFailedFn: statusFailedFn,
  59. ctx: ctx,
  60. cancel: cancel,
  61. }
  62. }
  63. func (monitor *HealthCheckMonitor) Start() {
  64. go monitor.checkWorker()
  65. }
  66. func (monitor *HealthCheckMonitor) Stop() {
  67. monitor.cancel()
  68. }
  69. func (monitor *HealthCheckMonitor) checkWorker() {
  70. for {
  71. ctx, cancel := context.WithDeadline(monitor.ctx, time.Now().Add(monitor.timeout))
  72. ok := monitor.doCheck(ctx)
  73. // check if this monitor has been closed
  74. select {
  75. case <-ctx.Done():
  76. cancel()
  77. return
  78. default:
  79. cancel()
  80. }
  81. if ok {
  82. if !monitor.statusOK && monitor.statusNormalFn != nil {
  83. monitor.statusOK = true
  84. monitor.statusNormalFn()
  85. }
  86. } else {
  87. monitor.failedTimes++
  88. if monitor.statusOK && int(monitor.failedTimes) >= monitor.maxFailedTimes && monitor.statusFailedFn != nil {
  89. monitor.statusOK = false
  90. monitor.statusFailedFn()
  91. }
  92. }
  93. time.Sleep(monitor.interval)
  94. }
  95. }
  96. func (monitor *HealthCheckMonitor) doCheck(ctx context.Context) bool {
  97. switch monitor.checkType {
  98. case "tcp":
  99. return monitor.doTcpCheck(ctx)
  100. case "http":
  101. return monitor.doHttpCheck(ctx)
  102. default:
  103. return false
  104. }
  105. }
  106. func (monitor *HealthCheckMonitor) doTcpCheck(ctx context.Context) bool {
  107. var d net.Dialer
  108. conn, err := d.DialContext(ctx, "tcp", monitor.addr)
  109. if err != nil {
  110. return false
  111. }
  112. conn.Close()
  113. return true
  114. }
  115. func (monitor *HealthCheckMonitor) doHttpCheck(ctx context.Context) bool {
  116. req, err := http.NewRequest("GET", monitor.url, nil)
  117. if err != nil {
  118. return false
  119. }
  120. resp, err := http.DefaultClient.Do(req)
  121. if err != nil {
  122. return false
  123. }
  124. if resp.StatusCode/100 != 2 {
  125. return false
  126. }
  127. return true
  128. }