peer_status.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2015 CoreOS, Inc.
  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. "fmt"
  17. "sync"
  18. "github.com/coreos/etcd/pkg/types"
  19. )
  20. type failureType struct {
  21. source string
  22. action string
  23. }
  24. type peerStatus struct {
  25. id types.ID
  26. mu sync.Mutex // protect active and failureMap
  27. active bool
  28. failureMap map[failureType]string
  29. }
  30. func newPeerStatus(id types.ID) *peerStatus {
  31. return &peerStatus{
  32. id: id,
  33. failureMap: make(map[failureType]string),
  34. }
  35. }
  36. func (s *peerStatus) activate() {
  37. s.mu.Lock()
  38. defer s.mu.Unlock()
  39. if !s.active {
  40. plog.Infof("the connection with %s became active", s.id)
  41. s.active = true
  42. s.failureMap = make(map[failureType]string)
  43. }
  44. }
  45. func (s *peerStatus) deactivate(failure failureType, reason string) {
  46. s.mu.Lock()
  47. defer s.mu.Unlock()
  48. if s.active {
  49. plog.Infof("the connection with %s became inactive", s.id)
  50. s.active = false
  51. }
  52. logline := fmt.Sprintf("failed to %s %s on %s (%s)", failure.action, s.id, failure.source, reason)
  53. if r, ok := s.failureMap[failure]; ok && r == reason {
  54. plog.Debugf(logline)
  55. return
  56. }
  57. s.failureMap[failure] = reason
  58. plog.Errorf(logline)
  59. }
  60. func (s *peerStatus) isActive() bool {
  61. s.mu.Lock()
  62. defer s.mu.Unlock()
  63. return s.active
  64. }