probing_status.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2015 The etcd Authors
  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 rafthttp
  15. import (
  16. "time"
  17. "github.com/xiang90/probing"
  18. )
  19. var (
  20. // proberInterval must be shorter than read timeout.
  21. // Or the connection will time-out.
  22. proberInterval = ConnReadTimeout - time.Second
  23. statusMonitoringInterval = 30 * time.Second
  24. statusErrorInterval = 5 * time.Second
  25. )
  26. func addPeerToProber(p probing.Prober, id string, us []string) {
  27. hus := make([]string, len(us))
  28. for i := range us {
  29. hus[i] = us[i] + ProbingPrefix
  30. }
  31. p.AddHTTP(id, proberInterval, hus)
  32. s, err := p.Status(id)
  33. if err != nil {
  34. plog.Errorf("failed to add peer %s into prober", id)
  35. } else {
  36. go monitorProbingStatus(s, id)
  37. }
  38. }
  39. func monitorProbingStatus(s probing.Status, id string) {
  40. // set the first interval short to log error early.
  41. interval := statusErrorInterval
  42. for {
  43. select {
  44. case <-time.After(interval):
  45. if !s.Health() {
  46. plog.Warningf("health check for peer %s could not connect: %v", id, s.Err())
  47. interval = statusErrorInterval
  48. } else {
  49. interval = statusMonitoringInterval
  50. }
  51. if s.ClockDiff() > time.Second {
  52. plog.Warningf("the clock difference against peer %s is too high [%v > %v]", id, s.ClockDiff(), time.Second)
  53. }
  54. rtts.WithLabelValues(id).Observe(s.SRTT().Seconds())
  55. case <-s.StopNotify():
  56. return
  57. }
  58. }
  59. }