-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathstore.go
76 lines (66 loc) · 1.7 KB
/
store.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
package protocol
import (
"context"
"sync"
)
// NodeRole identifies the role of a node.
type NodeRole int
// String implements the Stringer interface.
func (r NodeRole) String() string {
switch r {
case Voter:
return "voter"
case StandBy:
return "stand-by"
case Spare:
return "spare"
default:
return "unknown role"
}
}
// NodeInfo holds information about a single server.
type NodeInfo struct {
ID uint64
Address string
Role NodeRole
}
// NodeStore is used by a dqlite client to get an initial list of candidate
// dqlite servers that it can dial in order to find a leader server to connect
// to.
//
// Once connected, the client periodically updates the server addresses in the
// store by querying the leader about changes in the cluster (such as servers
// being added or removed).
type NodeStore interface {
// Get return the list of known servers.
Get(context.Context) ([]NodeInfo, error)
// Set updates the list of known cluster servers.
Set(context.Context, []NodeInfo) error
}
// InmemNodeStore keeps the list of servers in memory.
type InmemNodeStore struct {
mu sync.RWMutex
servers []NodeInfo
}
// NewInmemNodeStore creates NodeStore which stores its data in-memory.
func NewInmemNodeStore() *InmemNodeStore {
return &InmemNodeStore{
mu: sync.RWMutex{},
servers: make([]NodeInfo, 0),
}
}
// Get the current servers.
func (i *InmemNodeStore) Get(ctx context.Context) ([]NodeInfo, error) {
i.mu.RLock()
defer i.mu.RUnlock()
ret := make([]NodeInfo, len(i.servers))
copy(ret, i.servers)
return ret, nil
}
// Set the servers.
func (i *InmemNodeStore) Set(ctx context.Context, servers []NodeInfo) error {
i.mu.Lock()
defer i.mu.Unlock()
i.servers = servers
return nil
}