Skip to content

Commit 30500f3

Browse files
committed
remove goroutine for-loop index capture
1 parent 72bac57 commit 30500f3

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

part1/raft.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (cm *ConsensusModule) startElection() {
274274

275275
// Send RequestVote RPCs to all other servers concurrently.
276276
for _, peerId := range cm.peerIds {
277-
go func(peerId int) {
277+
go func() {
278278
args := RequestVoteArgs{
279279
Term: savedCurrentTerm,
280280
CandidateId: cm.id,
@@ -308,7 +308,7 @@ func (cm *ConsensusModule) startElection() {
308308
}
309309
}
310310
}
311-
}(peerId)
311+
}()
312312
}
313313

314314
// Run another election timer, in case this election is not successful.
@@ -368,7 +368,7 @@ func (cm *ConsensusModule) leaderSendHeartbeats() {
368368
Term: savedCurrentTerm,
369369
LeaderId: cm.id,
370370
}
371-
go func(peerId int) {
371+
go func() {
372372
cm.dlog("sending AppendEntries to %v: ni=%d, args=%+v", peerId, 0, args)
373373
var reply AppendEntriesReply
374374
if err := cm.server.Call(peerId, "ConsensusModule.AppendEntries", args, &reply); err == nil {
@@ -380,6 +380,6 @@ func (cm *ConsensusModule) leaderSendHeartbeats() {
380380
return
381381
}
382382
}
383-
}(peerId)
383+
}()
384384
}
385385
}

part2/raft.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ func (cm *ConsensusModule) startElection() {
374374

375375
// Send RequestVote RPCs to all other servers concurrently.
376376
for _, peerId := range cm.peerIds {
377-
go func(peerId int) {
377+
go func() {
378378
cm.mu.Lock()
379379
savedLastLogIndex, savedLastLogTerm := cm.lastLogIndexAndTerm()
380380
cm.mu.Unlock()
@@ -414,7 +414,7 @@ func (cm *ConsensusModule) startElection() {
414414
}
415415
}
416416
}
417-
}(peerId)
417+
}()
418418
}
419419

420420
// Run another election timer, in case this election is not successful.
@@ -475,7 +475,7 @@ func (cm *ConsensusModule) leaderSendHeartbeats() {
475475
cm.mu.Unlock()
476476

477477
for _, peerId := range cm.peerIds {
478-
go func(peerId int) {
478+
go func() {
479479
cm.mu.Lock()
480480
ni := cm.nextIndex[peerId]
481481
prevLogIndex := ni - 1
@@ -535,7 +535,7 @@ func (cm *ConsensusModule) leaderSendHeartbeats() {
535535
}
536536
}
537537
}
538-
}(peerId)
538+
}()
539539
}
540540
}
541541

part3/raft/raft.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ func (cm *ConsensusModule) startElection() {
480480

481481
// Send RequestVote RPCs to all other servers concurrently.
482482
for _, peerId := range cm.peerIds {
483-
go func(peerId int) {
483+
go func() {
484484
cm.mu.Lock()
485485
savedLastLogIndex, savedLastLogTerm := cm.lastLogIndexAndTerm()
486486
cm.mu.Unlock()
@@ -520,7 +520,7 @@ func (cm *ConsensusModule) startElection() {
520520
}
521521
}
522522
}
523-
}(peerId)
523+
}()
524524
}
525525

526526
// Run another election timer, in case this election is not successful.
@@ -608,7 +608,7 @@ func (cm *ConsensusModule) leaderSendAEs() {
608608
cm.mu.Unlock()
609609

610610
for _, peerId := range cm.peerIds {
611-
go func(peerId int) {
611+
go func() {
612612
cm.mu.Lock()
613613
ni := cm.nextIndex[peerId]
614614
prevLogIndex := ni - 1
@@ -697,7 +697,7 @@ func (cm *ConsensusModule) leaderSendAEs() {
697697
cm.mu.Unlock()
698698
}
699699
}
700-
}(peerId)
700+
}()
701701
}
702702
}
703703

0 commit comments

Comments
 (0)