raft: copy ents to avoid races

This commit is contained in:
Blake Mizerany 2014-08-22 15:08:13 -07:00 committed by Yicheng Qin
parent 71ed92b6bd
commit 6e6d81f094

View File

@ -85,7 +85,13 @@ func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
}
func (l *raftLog) unstableEnts() []Entry {
return l.entries(l.unstable)
ents := l.entries(l.unstable)
if ents == nil {
return nil
}
cpy := make([]Entry, len(ents))
copy(cpy, ents)
return cpy
}
func (l *raftLog) resetUnstable() {
@ -96,7 +102,13 @@ func (l *raftLog) resetUnstable() {
// all the returned entries will be marked as applied.
func (l *raftLog) nextEnts() (ents []Entry) {
if l.committed > l.applied {
return l.slice(l.applied+1, l.committed+1)
ents := l.slice(l.applied+1, l.committed+1)
if ents == nil {
return nil
}
cpy := make([]Entry, len(ents))
copy(cpy, ents)
return cpy
}
return nil
}