-
Notifications
You must be signed in to change notification settings - Fork 14
/
template_static.go
112 lines (98 loc) · 3.92 KB
/
template_static.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
// Code generated by protoc-gen-gorums. DO NOT EDIT.
// Source files can be found in: ./cmd/protoc-gen-gorums/dev
package gengorums
// pkgIdentMap maps from package name to one of the package's identifiers.
// These identifiers are used by the Gorums protoc plugin to generate import statements.
var pkgIdentMap = map[string]string{"fmt": "Errorf", "github.com/relab/gorums": "ConfigOption", "google.golang.org/grpc/encoding": "GetCodec"}
// reservedIdents holds the set of Gorums reserved identifiers.
// These identifiers cannot be used to define message types in a proto file.
var reservedIdents = []string{"Configuration", "Manager", "Node", "QuorumSpec"}
var staticCode = `// A Configuration represents a static set of nodes on which quorum remote
// procedure calls may be invoked.
type Configuration struct {
gorums.Configuration
qspec QuorumSpec
}
// Nodes returns a slice of each available node. IDs are returned in the same
// order as they were provided in the creation of the Manager.
func (c *Configuration) Nodes() []*Node {
nodes := make([]*Node, 0, c.Size())
for _, n := range c.Configuration {
nodes = append(nodes, &Node{n})
}
return nodes
}
// And returns a NodeListOption that can be used to create a new configuration combining c and d.
func (c Configuration) And(d *Configuration) gorums.NodeListOption {
return c.Configuration.And(d.Configuration)
}
// Except returns a NodeListOption that can be used to create a new configuration
// from c without the nodes in rm.
func (c Configuration) Except(rm *Configuration) gorums.NodeListOption {
return c.Configuration.Except(rm.Configuration)
}
func init() {
if encoding.GetCodec(gorums.ContentSubtype) == nil {
encoding.RegisterCodec(gorums.NewCodec())
}
}
// Manager maintains a connection pool of nodes on
// which quorum calls can be performed.
type Manager struct {
*gorums.Manager
}
// NewManager returns a new Manager for managing connection to nodes added
// to the manager. This function accepts manager options used to configure
// various aspects of the manager.
func NewManager(opts ...gorums.ManagerOption) (mgr *Manager) {
mgr = &Manager{}
mgr.Manager = gorums.NewManager(opts...)
return mgr
}
// NewConfiguration returns a configuration based on the provided list of nodes (required)
// and an optional quorum specification. The QuorumSpec is necessary for call types that
// must process replies. For configurations only used for unicast or multicast call types,
// a QuorumSpec is not needed. The QuorumSpec interface is also a ConfigOption.
// Nodes can be supplied using WithNodeMap or WithNodeList, or WithNodeIDs.
// A new configuration can also be created from an existing configuration,
// using the And, WithNewNodes, Except, and WithoutNodes methods.
func (m *Manager) NewConfiguration(opts ...gorums.ConfigOption) (c *Configuration, err error) {
if len(opts) < 1 || len(opts) > 2 {
return nil, fmt.Errorf("wrong number of options: %d", len(opts))
}
c = &Configuration{}
for _, opt := range opts {
switch v := opt.(type) {
case gorums.NodeListOption:
c.Configuration, err = gorums.NewConfiguration(m.Manager, v)
if err != nil {
return nil, err
}
case QuorumSpec:
// Must be last since v may match QuorumSpec if it is interface{}
c.qspec = v
default:
return nil, fmt.Errorf("unknown option type: %v", v)
}
}
// return an error if the QuorumSpec interface is not empty and no implementation was provided.
var test interface{} = struct{}{}
if _, empty := test.(QuorumSpec); !empty && c.qspec == nil {
return nil, fmt.Errorf("missing required QuorumSpec")
}
return c, nil
}
// Nodes returns a slice of available nodes on this manager.
// IDs are returned in the order they were added at creation of the manager.
func (m *Manager) Nodes() []*Node {
gorumsNodes := m.Manager.Nodes()
nodes := make([]*Node, 0, len(gorumsNodes))
for _, n := range gorumsNodes {
nodes = append(nodes, &Node{n})
}
return nodes
}
type Node struct {
*gorums.Node
}
`