-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
329 lines (277 loc) · 7.85 KB
/
Copy pathnode.go
File metadata and controls
329 lines (277 loc) · 7.85 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package aegis
import (
"context"
"encoding/json"
"fmt"
"net"
"time"
"github.com/zoobz-io/sctx"
)
// NodeType represents the type of node in the mesh.
type NodeType string
const (
// NodeTypeGeneric is the default node type.
NodeTypeGeneric NodeType = "generic"
)
// Node represents a node in the mesh network.
type Node struct {
ID string `json:"id"`
Name string `json:"name"`
Type NodeType `json:"type"`
Address string `json:"address"`
Services []ServiceInfo `json:"services,omitempty"`
Health *HealthInfo `json:"health"`
PeerManager *PeerManager `json:"-"`
MeshServer *MeshServer `json:"-"`
Topology *Topology `json:"-"`
TLSConfig *TLSConfig `json:"-"`
Admin sctx.Admin[Metadata] `json:"-"`
Guards *GuardRegistry `json:"-"`
nodeToken sctx.SignedToken
}
// NewNode creates a new mesh node.
func NewNode(id, name string, nodeType NodeType, address string) *Node {
node := &Node{
ID: id,
Name: name,
Type: nodeType,
Address: address,
Health: NewHealthInfo(),
}
node.PeerManager = NewPeerManager(id)
node.MeshServer = NewMeshServer(node)
node.Topology = NewTopology()
// Add self to topology
nodeInfo := NodeInfo{
ID: id,
Name: name,
Type: nodeType,
Address: address,
Services: node.Services,
}
_ = node.Topology.AddNode(nodeInfo)
return node
}
// EnableTLS enables TLS for the node using the specified certificate directory.
func (n *Node) EnableTLS(certDir string) error {
tlsConfig, err := LoadOrGenerateTLS(n.ID, certDir)
if err != nil {
return fmt.Errorf("failed to setup TLS: %w", err)
}
n.TLSConfig = tlsConfig
if n.MeshServer != nil {
n.MeshServer.SetTLSConfig(tlsConfig)
}
if n.PeerManager != nil {
n.PeerManager.SetTLSConfig(tlsConfig)
}
return nil
}
// String returns a string representation of the node.
func (n *Node) String() string {
return fmt.Sprintf("Node[%s:%s] %s @ %s", n.Type, n.ID, n.Name, n.Address)
}
// MarshalJSON implements json.Marshaler.
func (n *Node) MarshalJSON() ([]byte, error) {
type Alias Node
return json.Marshal((*Alias)(n))
}
// UnmarshalJSON implements json.Unmarshaler.
func (n *Node) UnmarshalJSON(data []byte) error {
type Alias Node
aux := &struct {
*Alias
}{
Alias: (*Alias)(n),
}
return json.Unmarshal(data, &aux)
}
// Validate validates the node configuration.
func (n *Node) Validate() error {
if n.ID == "" {
return fmt.Errorf("node ID cannot be empty")
}
if n.Name == "" {
return fmt.Errorf("node name cannot be empty")
}
if n.Address == "" {
return fmt.Errorf("node address cannot be empty")
}
_, _, err := net.SplitHostPort(n.Address)
if err != nil {
return fmt.Errorf("invalid address format: %w", err)
}
return nil
}
// SetHealth updates the node's health status.
func (n *Node) SetHealth(status HealthStatus, message string, err error) {
if n.Health == nil {
n.Health = NewHealthInfo()
}
n.Health.Update(status, message, err)
}
// GetHealth returns the node's health status and message.
func (n *Node) GetHealth() (HealthStatus, string) {
if n.Health == nil {
return HealthStatusUnknown, "Health not initialized"
}
status, _, message, errMsg := n.Health.Get()
if errMsg != "" {
return status, fmt.Sprintf("%s (Error: %s)", message, errMsg)
}
return status, message
}
// IsHealthy returns whether the node is healthy.
func (n *Node) IsHealthy() bool {
if n.Health == nil {
return false
}
return n.Health.IsHealthy()
}
// CheckHealth runs a health check using the provided checker.
func (n *Node) CheckHealth(ctx context.Context, checker HealthChecker) error {
if checker == nil {
n.SetHealth(HealthStatusUnhealthy, "No health checker provided", fmt.Errorf("health checker is nil"))
return fmt.Errorf("health checker is nil")
}
err := checker.Check(ctx)
if err != nil {
n.SetHealth(HealthStatusUnhealthy, fmt.Sprintf("Health check failed: %s", checker.Name()), err)
return err
}
n.SetHealth(HealthStatusHealthy, fmt.Sprintf("Health check passed: %s", checker.Name()), nil)
return nil
}
// StartServer starts the gRPC mesh server.
func (n *Node) StartServer() error {
if n.MeshServer == nil {
return fmt.Errorf("mesh server not initialized")
}
return n.MeshServer.Start()
}
// StopServer stops the gRPC mesh server.
func (n *Node) StopServer() {
if n.MeshServer != nil {
n.MeshServer.Stop()
}
}
// AddPeer adds a peer connection.
func (n *Node) AddPeer(info PeerInfo) error {
if n.PeerManager == nil {
return fmt.Errorf("peer manager not initialized")
}
return n.PeerManager.AddPeer(info)
}
// RemovePeer removes a peer connection.
func (n *Node) RemovePeer(peerID string) error {
if n.PeerManager == nil {
return fmt.Errorf("peer manager not initialized")
}
return n.PeerManager.RemovePeer(peerID)
}
// GetPeer returns a peer by ID.
func (n *Node) GetPeer(peerID string) (*Peer, bool) {
if n.PeerManager == nil {
return nil, false
}
return n.PeerManager.GetPeer(peerID)
}
// GetAllPeers returns all connected peers.
func (n *Node) GetAllPeers() []*Peer {
if n.PeerManager == nil {
return nil
}
return n.PeerManager.GetAllPeers()
}
// PingPeer sends a ping to a peer.
func (n *Node) PingPeer(ctx context.Context, peerID string) (*PingResponse, error) {
if n.PeerManager == nil {
return nil, fmt.Errorf("peer manager not initialized")
}
return n.PeerManager.PingPeer(ctx, peerID)
}
// GetPeerHealth retrieves the health status of a peer.
func (n *Node) GetPeerHealth(ctx context.Context, peerID string) (*HealthResponse, error) {
if n.PeerManager == nil {
return nil, fmt.Errorf("peer manager not initialized")
}
return n.PeerManager.GetPeerHealth(ctx, peerID)
}
// GetPeerNodeInfo retrieves node information from a peer.
func (n *Node) GetPeerNodeInfo(ctx context.Context, peerID string) (*NodeInfoResponse, error) {
if n.PeerManager == nil {
return nil, fmt.Errorf("peer manager not initialized")
}
return n.PeerManager.GetPeerNodeInfo(ctx, peerID)
}
// GetTopologyVersion returns the current topology version.
func (n *Node) GetTopologyVersion() int64 {
if n.Topology == nil {
return 0
}
return n.Topology.GetVersion()
}
// GetMeshNodes returns all nodes in the topology.
func (n *Node) GetMeshNodes() []NodeInfo {
if n.Topology == nil {
return nil
}
return n.Topology.GetAllNodes()
}
// SyncTopology synchronizes topology with a specific peer.
func (n *Node) SyncTopology(ctx context.Context, peerID string) error {
if n.Topology == nil || n.PeerManager == nil {
return fmt.Errorf("topology or peer manager not initialized")
}
peer, exists := n.GetPeer(peerID)
if !exists {
return fmt.Errorf("peer %s not found", peerID)
}
req := &TopologySyncRequest{
SenderId: n.ID,
Version: n.Topology.GetVersion(),
}
resp, err := peer.Client.SyncTopology(ctx, req)
if err != nil {
return err
}
if resp.Version > n.Topology.GetVersion() {
newTopology := NewTopology()
for _, nodeProto := range resp.Nodes {
_ = newTopology.AddNode(protoToNodeInfo(nodeProto))
}
newTopology.Version = resp.Version
newTopology.UpdatedAt = time.Unix(resp.UpdatedAt, 0)
n.Topology.Merge(newTopology)
}
return nil
}
// SyncTopologyWithAllPeers synchronizes topology with all connected peers.
func (n *Node) SyncTopologyWithAllPeers(ctx context.Context) error {
if n.Topology == nil || n.PeerManager == nil {
return fmt.Errorf("topology or peer manager not initialized")
}
peers := n.GetAllPeers()
var lastErr error
for _, peer := range peers {
if err := n.SyncTopology(ctx, peer.Info.ID); err != nil {
lastErr = err
}
}
return lastErr
}
// Guard registers a guard for a gRPC method on this node.
func (n *Node) Guard(method string, guard sctx.Guard) {
if n.Guards == nil {
n.Guards = NewGuardRegistry()
}
n.Guards.Register(method, guard)
}
// Shutdown gracefully shuts down the node.
func (n *Node) Shutdown() error {
n.StopServer()
if n.PeerManager != nil {
return n.PeerManager.Close()
}
return nil
}