contrib/raftexample: follow pipeline guidelines closer

close raft commit channel before issuing raft error since it's done
sending
This commit is contained in:
chz
2015-12-20 11:01:41 -08:00
committed by Anthony Romano
parent b7cf2385e6
commit b73a11ff45
3 changed files with 26 additions and 27 deletions

View File

@ -58,9 +58,7 @@ func (s *kvstore) Propose(k string, v string) {
} }
func (s *kvstore) readCommits(commitC <-chan *string, errorC <-chan error) { func (s *kvstore) readCommits(commitC <-chan *string, errorC <-chan error) {
for { for data := range commitC {
select {
case data := <-commitC:
if data == nil { if data == nil {
// done replaying log; new data incoming // done replaying log; new data incoming
return return
@ -74,9 +72,8 @@ func (s *kvstore) readCommits(commitC <-chan *string, errorC <-chan error) {
s.mu.Lock() s.mu.Lock()
s.kvStore[data_kv.Key] = data_kv.Val s.kvStore[data_kv.Key] = data_kv.Val
s.mu.Unlock() s.mu.Unlock()
case err := <-errorC:
log.Println(err)
return
} }
if err, ok := <-errorC; ok {
log.Fatal(err)
} }
} }

View File

@ -122,12 +122,8 @@ func (rc *raftNode) replayWAL() *wal.WAL {
} }
func (rc *raftNode) writeError(err error) { func (rc *raftNode) writeError(err error) {
rc.errorC <- err
rc.stop()
}
func (rc *raftNode) stop() {
close(rc.commitC) close(rc.commitC)
rc.errorC <- err
close(rc.errorC) close(rc.errorC)
rc.node.Stop() rc.node.Stop()
} }
@ -214,7 +210,9 @@ func (rc *raftNode) serveChannels() {
return return
case <-stopc: case <-stopc:
rc.stop() close(rc.commitC)
close(rc.errorC)
rc.node.Stop()
return return
} }
} }

View File

@ -87,11 +87,15 @@ func TestProposeOnCommit(t *testing.T) {
// feedback for "n" committed entries, then update donec // feedback for "n" committed entries, then update donec
go func(pC chan<- string, cC <-chan *string, eC <-chan error) { go func(pC chan<- string, cC <-chan *string, eC <-chan error) {
for n := 0; n < 100; n++ { for n := 0; n < 100; n++ {
s, ok := <-cC
if !ok {
pC = nil
}
select { select {
case s := <-cC: case pC <- *s:
pC <- *s continue
case err, _ := <-eC: case err, _ := <-eC:
t.Fatalf("eC closed (%v)", err) t.Fatalf("eC message (%v)", err)
} }
} }
donec <- struct{}{} donec <- struct{}{}