|
|
@ -6,7 +6,7 @@ import ( |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type CheckerMgr struct { |
|
|
|
type CheckerMgr struct { |
|
|
|
checkers map[string]*checker |
|
|
|
checkers map[string]CheckerHandler |
|
|
|
ctx context.Context |
|
|
|
ctx context.Context |
|
|
|
cancel func() |
|
|
|
cancel func() |
|
|
|
watchers map[uint64]chan string |
|
|
|
watchers map[uint64]chan string |
|
|
@ -18,7 +18,7 @@ type CheckerMgr struct { |
|
|
|
func New(ctx context.Context) *CheckerMgr { |
|
|
|
func New(ctx context.Context) *CheckerMgr { |
|
|
|
c, cancel := context.WithCancel(ctx) |
|
|
|
c, cancel := context.WithCancel(ctx) |
|
|
|
return &CheckerMgr{ |
|
|
|
return &CheckerMgr{ |
|
|
|
checkers: make(map[string]*checker), |
|
|
|
checkers: make(map[string]CheckerHandler), |
|
|
|
ctx: c, |
|
|
|
ctx: c, |
|
|
|
cancel: cancel, |
|
|
|
cancel: cancel, |
|
|
|
lock: sync.RWMutex{}, |
|
|
|
lock: sync.RWMutex{}, |
|
|
@ -53,7 +53,7 @@ func (c *CheckerMgr) GetStatus(name ...string) []StatusResult { |
|
|
|
if len(name) == 0 { |
|
|
|
if len(name) == 0 { |
|
|
|
for _, v := range c.checkers { |
|
|
|
for _, v := range c.checkers { |
|
|
|
status = append(status, StatusResult{ |
|
|
|
status = append(status, StatusResult{ |
|
|
|
Name: v.Name, |
|
|
|
Name: v.getName(), |
|
|
|
CheckerStatus: v.getStatus(), |
|
|
|
CheckerStatus: v.getStatus(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -61,7 +61,7 @@ func (c *CheckerMgr) GetStatus(name ...string) []StatusResult { |
|
|
|
for _, n := range name { |
|
|
|
for _, n := range name { |
|
|
|
if v, ok := c.checkers[n]; ok { |
|
|
|
if v, ok := c.checkers[n]; ok { |
|
|
|
status = append(status, StatusResult{ |
|
|
|
status = append(status, StatusResult{ |
|
|
|
Name: v.Name, |
|
|
|
Name: v.getName(), |
|
|
|
CheckerStatus: v.getStatus(), |
|
|
|
CheckerStatus: v.getStatus(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
@ -70,12 +70,12 @@ func (c *CheckerMgr) GetStatus(name ...string) []StatusResult { |
|
|
|
return status |
|
|
|
return status |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *CheckerMgr) RegisterChecker(checker2 *checker) { |
|
|
|
func (c *CheckerMgr) RegisterChecker(checker CheckerHandler) { |
|
|
|
c.checkers[checker2.Name] = checker2 |
|
|
|
c.checkers[checker.getName()] = checker |
|
|
|
checker2.setWatcher(c) |
|
|
|
checker.setNotifier(c) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *CheckerMgr) Watch(name string) { |
|
|
|
func (c *CheckerMgr) notify(name string) { |
|
|
|
c.lock.RLock() |
|
|
|
c.lock.RLock() |
|
|
|
defer c.lock.RUnlock() |
|
|
|
defer c.lock.RUnlock() |
|
|
|
for _, ch := range c.watchers { |
|
|
|
for _, ch := range c.watchers { |
|
|
@ -86,24 +86,24 @@ func (c *CheckerMgr) Watch(name string) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
type WatcherResult struct { |
|
|
|
type Watcher struct { |
|
|
|
id uint64 |
|
|
|
id uint64 |
|
|
|
Ch <-chan string |
|
|
|
Ch <-chan string |
|
|
|
c *CheckerMgr |
|
|
|
c *CheckerMgr |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (w *WatcherResult) Close() { |
|
|
|
func (w *Watcher) Close() { |
|
|
|
w.c.closeWatcher(w.id) |
|
|
|
w.c.closeWatcher(w.id) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (c *CheckerMgr) NewWatcher() WatcherResult { |
|
|
|
func (c *CheckerMgr) NewWatcher() Watcher { |
|
|
|
c.lock.Lock() |
|
|
|
c.lock.Lock() |
|
|
|
wID := c.watcherID |
|
|
|
wID := c.watcherID |
|
|
|
c.watcherID++ |
|
|
|
c.watcherID++ |
|
|
|
ch := make(chan string, 1) |
|
|
|
ch := make(chan string, 1) |
|
|
|
c.watchers[wID] = ch |
|
|
|
c.watchers[wID] = ch |
|
|
|
c.lock.Unlock() |
|
|
|
c.lock.Unlock() |
|
|
|
return WatcherResult{ |
|
|
|
return Watcher{ |
|
|
|
id: wID, |
|
|
|
id: wID, |
|
|
|
Ch: ch, |
|
|
|
Ch: ch, |
|
|
|
c: c, |
|
|
|
c: c, |
|
|
|