feat: 优雅退出

This commit is contained in:
tbphp
2025-07-13 20:55:32 +08:00
parent 99fd68d08f
commit ae751eb48a
8 changed files with 142 additions and 54 deletions

View File

@@ -79,18 +79,28 @@ func (s *LeaderLock) Start() error {
}
// Stop gracefully stops the leadership maintenance process.
func (s *LeaderLock) Stop() {
func (s *LeaderLock) Stop(ctx context.Context) {
if s.isSingleNode {
return
}
logrus.Info("Stopping leadership maintenance process...")
close(s.stopChan)
s.wg.Wait()
done := make(chan struct{})
go func() {
s.wg.Wait()
close(done)
}()
select {
case <-done:
logrus.Info("Leadership maintenance process stopped gracefully.")
case <-ctx.Done():
logrus.Warn("Leadership maintenance process stop timed out.")
}
if s.isLeader.Load() {
s.releaseLock()
}
logrus.Info("Leadership maintenance process stopped.")
}
// IsLeader returns true if the current node is the leader.
@@ -176,7 +186,6 @@ func (s *LeaderLock) maintainLeadershipLoop() {
logrus.WithError(err).Warn("Error during leadership maintenance cycle.")
}
case <-s.stopChan:
logrus.Info("Leadership maintenance loop stopping.")
return
}
}