-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathlearner.go
57 lines (50 loc) · 903 Bytes
/
learner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package paxos
import (
"log"
"net"
"net/rpc"
"sync"
)
type Learner struct {
mu sync.Mutex
localAddr string
acceptorCount map[float32]int
acceptedValue map[float32]interface{}
decidedValue interface{}
quorumSize int
listener net.Listener
}
func (le *Learner) getQuorumSize() int {
le.mu.Lock()
size := le.quorumSize
le.mu.Unlock()
return size
}
func (le *Learner) RecieveAccepted(arg *AcceptedMsg, reply *EmptyMsg) error {
// PASS
return nil
}
func (le *Learner) startRpc() {
rpcx := rpc.NewServer()
rpcx.Register(le)
l, e := net.Listen("tcp", le.localAddr)
le.listener = l
if e != nil {
log.Fatal("listen error:", e)
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
return
}
go rpcx.ServeConn(conn)
}
}()
}
func (le *Learner) clean() {
le.acceptedValue = nil
}
func (le *Learner) close() {
le.listener.Close()
}