forked from rqlite/rqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peers.go
49 lines (42 loc) · 971 Bytes
/
peers.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
package store
import (
"bytes"
"encoding/json"
"io/ioutil"
"os"
"path/filepath"
)
const (
jsonPeerPath = "peers.json"
)
// NumPeers returns the number of peers indicated by the config files
// within raftDir.
//
// This code makes assumptions about how the Raft module works.
func NumPeers(raftDir string) (int, error) {
// Read the file
buf, err := ioutil.ReadFile(filepath.Join(raftDir, jsonPeerPath))
if err != nil && !os.IsNotExist(err) {
return 0, err
}
// Check for no peers
if len(buf) == 0 {
return 0, nil
}
// Decode the peers
var peerSet []string
dec := json.NewDecoder(bytes.NewReader(buf))
if err := dec.Decode(&peerSet); err != nil {
return 0, err
}
return len(peerSet), nil
}
// JoinAllowed returns whether the config files within raftDir indicate
// that the node can join a cluster.
func JoinAllowed(raftDir string) (bool, error) {
n, err := NumPeers(raftDir)
if err != nil {
return false, err
}
return n <= 1, nil
}