Skip to content

Commit

Permalink
raft: refactor inflight
Browse files Browse the repository at this point in the history
  • Loading branch information
xiang90 authored and gyuho committed Sep 23, 2016
1 parent cf09562 commit 9634f71
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions raft/progress.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,17 @@ type Progress struct {
RecentActive bool

// inflights is a sliding window for the inflight messages.
// Each inflight message might contain more than one entries
// and has a size limit defined in raft config as MaxSizePerMsg.
// Thus inflight effectively limits both the number of inflight messages
// and the bandwidth each Progress can use.
// When inflights is full, no more message should be sent.
// When a leader sends out a message, the index of the last
// entry should be added to inflights. The index MUST be added
// into inflights in order.
// When a leader receives a reply, the previous inflights should
// be freed by calling inflights.freeTo.
// be freed by calling inflights.freeTo with the index of the last
// received entry.
ins *inflights
}

Expand Down Expand Up @@ -183,7 +188,10 @@ type inflights struct {
count int

// the size of the buffer
size int
size int

// buffer contains the index of the last entry
// inside one message.
buffer []uint64
}

Expand All @@ -199,8 +207,9 @@ func (in *inflights) add(inflight uint64) {
panic("cannot add into a full inflights")
}
next := in.start + in.count
if next >= in.size {
next -= in.size
size := in.size
if next >= size {
next -= size
}
if next >= len(in.buffer) {
in.growBuf()
Expand Down Expand Up @@ -236,10 +245,10 @@ func (in *inflights) freeTo(to uint64) {
if to < in.buffer[idx] { // found the first large inflight
break
}

size := in.size
// increase index and maybe rotate
if idx++; idx >= in.size {
idx -= in.size
if idx++; idx >= size {
idx -= size
}
}
// free i inflights and set new start index
Expand Down

0 comments on commit 9634f71

Please sign in to comment.