Skip to content

Commit

Permalink
etcdserver: export method EtcdServer.leaderChangedNotify (#12378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanhao committed Feb 2, 2021
1 parent e897daa commit 895b9e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion server/etcdserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,15 @@ type Server interface {
ClusterVersion() *semver.Version
Cluster() api.Cluster
Alarms() []*pb.AlarmMember

// LeaderChangedNotify returns a channel for application level code to be notified
// when etcd leader changes, this function is intend to be used only in application
// which embed etcd.
// Caution:
// 1. the returned channel is being closed when the leadership changes.
// 2. so the new channel needs to be obtained for each raft term.
// 3. user can loose some consecutive channel changes using this API.
LeaderChangedNotify() <-chan struct{}
}

// EtcdServer is the production implementation of the Server interface
Expand Down Expand Up @@ -1743,7 +1752,7 @@ func (s *EtcdServer) getLead() uint64 {
return atomic.LoadUint64(&s.lead)
}

func (s *EtcdServer) leaderChangedNotify() <-chan struct{} {
func (s *EtcdServer) LeaderChangedNotify() <-chan struct{} {
s.leaderChangedMu.RLock()
defer s.leaderChangedMu.RUnlock()
return s.leaderChanged
Expand Down
2 changes: 1 addition & 1 deletion server/etcdserver/v3_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func (s *EtcdServer) linearizableReadLoop() {
ctxToSend := make([]byte, 8)
id1 := s.reqIDGen.Next()
binary.BigEndian.PutUint64(ctxToSend, id1)
leaderChangedNotifier := s.leaderChangedNotify()
leaderChangedNotifier := s.LeaderChangedNotify()
select {
case <-leaderChangedNotifier:
continue
Expand Down

0 comments on commit 895b9e5

Please sign in to comment.