浏览代码

raft: example

Blake Mizerany 11 年之前
父节点
当前提交
ff14d1de52
共有 1 个文件被更改,包括 41 次插入0 次删除
  1. 41 0
      raft2/example_test.go

+ 41 - 0
raft2/example_test.go

@@ -0,0 +1,41 @@
+package raft
+
+import (
+	"log"
+
+	"code.google.com/p/go.net/context"
+)
+
+func apply(e Entry)               {}
+func sendMessages(msgs []Message) {}
+func saveToDisk(ents []Entry)     {}
+
+func Example_Node() {
+	n := Start(context.Background(), "", 0, 0)
+
+	// stuff to n happens in other gorotines
+
+	// a cache of entries that have been saved to disk, but not yet
+	// committed the the store
+	var cents []Entry
+	for {
+		// ReadState blocks until there is new state ready.
+		st, ents, msgs, err := n.ReadState()
+		if err != nil {
+			log.Fatal(err)
+		}
+
+		saveToDisk(ents)
+
+		cents = append(cents, ents...)
+		for i, e := range cents {
+			if e.Index > st.Commit {
+				cents = cents[i:]
+				break
+			}
+			apply(e)
+		}
+
+		sendMessages(msgs)
+	}
+}