-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathself.go
84 lines (72 loc) · 2.15 KB
/
self.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
// Package fleet provides a distributed peer-to-peer communication framework.
package fleet
import "sync"
// Global singleton instance management
// These variables manage the globally accessible Agent instance
var (
// self holds the global Agent instance
self *Agent
// selfLk protects access to the self variable
selfLk sync.RWMutex
// selfCond is a condition variable for notifying when self becomes available
selfCond = sync.NewCond(selfLk.RLocker())
)
// Self returns the global Agent instance that was created by New().
// If no Agent has been created yet, this function will block until
// one is available.
//
// IMPORTANT: Due to the blocking behavior, this function should not be
// used in init() functions. Only use it in goroutines or after an Agent
// has been explicitly created.
//
// Returns:
// - The global Agent instance
func Self() *Agent {
selfLk.RLock()
defer selfLk.RUnlock()
// Wait until an Agent instance is available
for {
if self != nil {
return self
}
selfCond.Wait() // Block until setSelf() calls Broadcast()
}
}
// selfNoWait returns the global Agent instance without waiting.
// This returns nil if no Agent has been created yet.
//
// Returns:
// - The global Agent instance, or nil if not available
func selfNoWait() *Agent {
selfLk.RLock()
defer selfLk.RUnlock()
return self
}
// setSelf sets the global Agent instance.
// This is called during Agent initialization to make the instance
// globally available and wake up any goroutines waiting in Self().
//
// Parameters:
// - a: The Agent instance to set as the global instance
func setSelf(a *Agent) {
selfLk.Lock()
defer selfLk.Unlock()
// Only set if not already set
if self == nil {
self = a
}
// Notify all waiters that an Agent is now available
selfCond.Broadcast()
}
// IsReady returns whether the fleet subsystem is fully initialized and ready.
// This checks both that an Agent exists and that it reports ready status.
//
// Returns:
// - true if the fleet is fully initialized and ready, false otherwise
func IsReady() bool {
a := selfNoWait()
if a == nil {
return false // No Agent exists
}
return a.GetStatus() == 1 // Status 1 means ready
}