diff --git a/internal/util/sessionutil/session_util.go b/internal/util/sessionutil/session_util.go index 7a97acabee..f17a3d145d 100644 --- a/internal/util/sessionutil/session_util.go +++ b/internal/util/sessionutil/session_util.go @@ -794,7 +794,7 @@ func (s *Session) LivenessCheck(ctx context.Context, callback func()) { case mvccpb.PUT: log.Info("register session success", zap.String("role", s.ServerName), zap.String("key", string(event.Kv.Key))) case mvccpb.DELETE: - if s.retryKeepAlive.Load().(bool) { + if s.isRetryingKeepAlive() { log.Info("session key is deleted during re register, ignore this DELETE event", zap.String("role", s.ServerName), zap.String("key", string(event.Kv.Key))) continue } @@ -873,6 +873,14 @@ func (s *Session) SetEnableRetryKeepAlive(enable bool) { s.enableRetryKeepAlive = enable } +func (s *Session) isRetryingKeepAlive() bool { + v, ok := s.retryKeepAlive.Load().(bool) + if !ok { + return false + } + return v +} + func (s *Session) safeCloseLiveCh() { s.liveChOnce.Do(func() { close(s.liveCh) diff --git a/internal/util/sessionutil/session_util_test.go b/internal/util/sessionutil/session_util_test.go index 8f6d6695da..009da81ff9 100644 --- a/internal/util/sessionutil/session_util_test.go +++ b/internal/util/sessionutil/session_util_test.go @@ -797,6 +797,29 @@ func (s *SessionSuite) TestDisconnected() { } } +func (s *SessionSuite) TestRetryKeepAlive() { + st := &Session{} + st.retryKeepAlive.Store(true) + sf := &Session{} + sf.retryKeepAlive.Store(false) + + cases := []struct { + tag string + input *Session + expect bool + }{ + {"not_set", &Session{}, false}, + {"set_true", st, true}, + {"set_false", sf, false}, + } + + for _, c := range cases { + s.Run(c.tag, func() { + s.Equal(c.expect, c.input.isRetryingKeepAlive()) + }) + } +} + func (s *SessionSuite) TestGoingStop() { ctx := context.Background() sdisconnect := NewSession(ctx, s.metaRoot, s.client)