fix(raft): electionLoop 复用 timer, 按 Stop→drain→Reset 顺序更新#40
Merged
Conversation
之前每次循环 time.NewTimer, 既浪费 GC, 又使 heartbeatCh/electionCh 触发时新随机的 timeout 跟旧的 timeout 不一致, 偏离 Raft 规范 (election 超时应在每轮 election 结束后重新选取, 而不是每次心跳)。 - NewRaftWithDataDir 内 time.NewTimer 一次, 用 r.electionTimeout - electionLoop 仅 select; 抽出 resetElectionTimer 用标准 Stop→drain→Reset 三步 - timeout 在每次 reset 时重新随机, 既保留原行为又避免 timer leak Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughElection timer management is refactored from allocating a fresh timer on each loop iteration to reusing a single timer object. The constructor initializes the timer, and the election loop uses, resets, and re-arms it across multiple cycles while properly draining stale signals. ChangesElection Timer Reuse
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Background
原代码 `for { r.timer = time.NewTimer(timeout); select{...} }` 每轮新建 timer, GC 压力 + 偶发的旧 timer fire 写入新 timer 的 channel 风险。改为复用单一 timer + 标准 Stop/drain/Reset 序列, 符合 Go 官方 timer.Reset 文档建议。
Test plan
🤖 Generated with Claude Code
Summary by CodeRabbit