leader.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright 2017 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 grpcproxy
  15. import (
  16. "context"
  17. "math"
  18. "sync"
  19. "go.etcd.io/etcd/clientv3"
  20. "golang.org/x/time/rate"
  21. )
  22. const (
  23. lostLeaderKey = "__lostleader" // watched to detect leader loss
  24. retryPerSecond = 10
  25. )
  26. type leader struct {
  27. ctx context.Context
  28. w clientv3.Watcher
  29. // mu protects leaderc updates.
  30. mu sync.RWMutex
  31. leaderc chan struct{}
  32. disconnc chan struct{}
  33. donec chan struct{}
  34. }
  35. func newLeader(ctx context.Context, w clientv3.Watcher) *leader {
  36. l := &leader{
  37. ctx: clientv3.WithRequireLeader(ctx),
  38. w: w,
  39. leaderc: make(chan struct{}),
  40. disconnc: make(chan struct{}),
  41. donec: make(chan struct{}),
  42. }
  43. // begin assuming leader is lost
  44. close(l.leaderc)
  45. go l.recvLoop()
  46. return l
  47. }
  48. func (l *leader) recvLoop() {
  49. defer close(l.donec)
  50. limiter := rate.NewLimiter(rate.Limit(retryPerSecond), retryPerSecond)
  51. rev := int64(math.MaxInt64 - 2)
  52. for limiter.Wait(l.ctx) == nil {
  53. wch := l.w.Watch(l.ctx, lostLeaderKey, clientv3.WithRev(rev), clientv3.WithCreatedNotify())
  54. cresp, ok := <-wch
  55. if !ok {
  56. l.loseLeader()
  57. continue
  58. }
  59. if cresp.Err() != nil {
  60. l.loseLeader()
  61. if clientv3.IsConnCanceled(cresp.Err()) {
  62. close(l.disconnc)
  63. return
  64. }
  65. continue
  66. }
  67. l.gotLeader()
  68. <-wch
  69. l.loseLeader()
  70. }
  71. }
  72. func (l *leader) loseLeader() {
  73. l.mu.RLock()
  74. defer l.mu.RUnlock()
  75. select {
  76. case <-l.leaderc:
  77. default:
  78. close(l.leaderc)
  79. }
  80. }
  81. // gotLeader will force update the leadership status to having a leader.
  82. func (l *leader) gotLeader() {
  83. l.mu.Lock()
  84. defer l.mu.Unlock()
  85. select {
  86. case <-l.leaderc:
  87. l.leaderc = make(chan struct{})
  88. default:
  89. }
  90. }
  91. func (l *leader) disconnectNotify() <-chan struct{} { return l.disconnc }
  92. func (l *leader) stopNotify() <-chan struct{} { return l.donec }
  93. // lostNotify returns a channel that is closed if there has been
  94. // a leader loss not yet followed by a leader reacquire.
  95. func (l *leader) lostNotify() <-chan struct{} {
  96. l.mu.RLock()
  97. defer l.mu.RUnlock()
  98. return l.leaderc
  99. }