-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
147 lines (124 loc) · 2.66 KB
/
server.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package raft
import (
"fmt"
"log"
"net"
"net/rpc"
"sync"
)
type Server struct {
mu sync.Mutex
serverId int
peerIds []int
cm *ConsensusModule
rpcProxy *RPCProxy
storage Storage
rpcServer *rpc.Server
listener net.Listener
commitChan chan<- CommitEntry
peerClients map[int]*rpc.Client
ready <-chan interface{}
quit chan interface{}
wg sync.WaitGroup
}
func NewServer(serverId int, peerIds []int, storage Storage, ready <-chan interface{}, commitChan chan<- CommitEntry) *Server {
s := new(Server)
s.serverId = serverId
s.peerIds = peerIds
s.peerClients = make(map[int]*rpc.Client)
s.ready = ready
s.quit = make(chan interface{})
s.commitChan = commitChan
s.storage = storage
return s
}
func (s *Server) Serve() {
s.mu.Lock()
s.cm = NewConsensusModule(s.serverId, s.peerIds, s, s.ready, s.storage, s.commitChan)
s.rpcServer = rpc.NewServer()
s.rpcProxy = &RPCProxy{cm: s.cm}
err := s.rpcServer.RegisterName("ConsensusModule", s.rpcProxy)
if err != nil {
log.Fatal(err)
}
s.listener, err = net.Listen("tcp", ":0")
if err != nil {
log.Fatal(err)
}
log.Printf("[%v] listening at %s", s.serverId, s.listener.Addr())
s.mu.Unlock()
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
conn, err := s.listener.Accept()
if err != nil {
select {
case <-s.quit:
return
default:
log.Fatal("accept error:", err)
}
}
s.wg.Add(1)
go func() {
s.rpcServer.ServeConn(conn)
s.wg.Done()
}()
}
}()
}
func (s *Server) DisconnectAll() {
s.mu.Lock()
defer s.mu.Unlock()
for id := range s.peerClients {
if s.peerClients[id] != nil {
_ = s.peerClients[id].Close()
s.peerClients[id] = nil
}
}
}
func (s *Server) DisconnectPeer(peerId int) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.peerClients[peerId] != nil {
err := s.peerClients[peerId].Close()
if err != nil {
return err
}
s.peerClients[peerId] = nil
}
return nil
}
func (s *Server) Shutdown() {
s.cm.Stop()
close(s.quit)
_ = s.listener.Close()
s.wg.Wait()
}
func (s *Server) GetListenAddr() net.Addr {
s.mu.Lock()
defer s.mu.Unlock()
return s.listener.Addr()
}
func (s *Server) ConnectToPeer(peerId int, addr net.Addr) error {
s.mu.Lock()
defer s.mu.Unlock()
if s.peerClients[peerId] == nil {
client, err := rpc.Dial(addr.Network(), addr.String())
if err != nil {
return err
}
s.peerClients[peerId] = client
}
return nil
}
func (s *Server) Call(id int, serviceMethod string, args interface{}, reply interface{}) error {
s.mu.Lock()
peer := s.peerClients[id]
s.mu.Unlock()
if peer == nil {
return fmt.Errorf("call client %d after it's closed", id)
}
return peer.Call(serviceMethod, args, reply)
}