Skip to content
Snippets Groups Projects
Commit bb751690 authored by Jono Wenger's avatar Jono Wenger Committed by Benjamin Wenger
Browse files

XX-3354 / add unregister to health tracker

parent 99528327
No related branches found
No related tags found
1 merge request!23Release
...@@ -256,10 +256,15 @@ func (c *Client) IsNetworkHealthy() bool { ...@@ -256,10 +256,15 @@ func (c *Client) IsNetworkHealthy() bool {
return c.api.GetHealth().IsHealthy() return c.api.GetHealth().IsHealthy()
} }
// registers the network health callback to be called any time the network // RegisterNetworkHealthCB registers the network health callback to be called
// health changes // any time the network health changes. Returns a unique ID that can be used to
func (c *Client) RegisterNetworkHealthCB(nhc NetworkHealthCallback) { // unregister the network health callback.
c.api.GetHealth().AddFunc(nhc.Callback) func (c *Client) RegisterNetworkHealthCB(nhc NetworkHealthCallback) uint64 {
return c.api.GetHealth().AddFunc(nhc.Callback)
}
func (c *Client) UnregisterNetworkHealthCB(funcID uint64) {
c.api.GetHealth().RemoveFunc(funcID)
} }
// RegisterListener records and installs a listener for messages // RegisterListener records and installs a listener for messages
......
...@@ -8,8 +8,10 @@ ...@@ -8,8 +8,10 @@
package interfaces package interfaces
type HealthTracker interface { type HealthTracker interface {
AddChannel(chan bool) AddChannel(chan bool) uint64
AddFunc(f func(bool)) RemoveChannel(uint64)
AddFunc(f func(bool)) uint64
RemoveFunc(uint64)
IsHealthy() bool IsHealthy() bool
WasHealthy() bool WasHealthy() bool
} }
...@@ -24,8 +24,10 @@ type Tracker struct { ...@@ -24,8 +24,10 @@ type Tracker struct {
heartbeat chan network.Heartbeat heartbeat chan network.Heartbeat
channels []chan bool channels map[uint64]chan bool
funcs []func(isHealthy bool) funcs map[uint64]func(isHealthy bool)
channelsID uint64
funcsID uint64
running bool running bool
...@@ -51,7 +53,8 @@ func Init(instance *network.Instance, timeout time.Duration) *Tracker { ...@@ -51,7 +53,8 @@ func Init(instance *network.Instance, timeout time.Duration) *Tracker {
func newTracker(timeout time.Duration) *Tracker { func newTracker(timeout time.Duration) *Tracker {
return &Tracker{ return &Tracker{
timeout: timeout, timeout: timeout,
channels: []chan bool{}, channels: map[uint64]chan bool{},
funcs: map[uint64]func(isHealthy bool){},
heartbeat: make(chan network.Heartbeat, 100), heartbeat: make(chan network.Heartbeat, 100),
isHealthy: false, isHealthy: false,
running: false, running: false,
...@@ -59,26 +62,56 @@ func newTracker(timeout time.Duration) *Tracker { ...@@ -59,26 +62,56 @@ func newTracker(timeout time.Duration) *Tracker {
} }
// AddChannel adds a channel to the list of Tracker channels such that each // AddChannel adds a channel to the list of Tracker channels such that each
// channel can be notified of network changes. // channel can be notified of network changes. Returns a unique ID for the
func (t *Tracker) AddChannel(c chan bool) { // channel.
func (t *Tracker) AddChannel(c chan bool) uint64 {
var currentID uint64
t.mux.Lock() t.mux.Lock()
t.channels = append(t.channels, c) t.channels[t.channelsID] = c
currentID = t.channelsID
t.channelsID++
t.mux.Unlock() t.mux.Unlock()
select { select {
case c <- t.IsHealthy(): case c <- t.IsHealthy():
default: default:
} }
return currentID
}
// RemoveChannel removes the channel with the given ID from the list of Tracker
// channels so that it will not longer be notified of network changes.
func (t *Tracker) RemoveChannel(chanID uint64) {
t.mux.Lock()
delete(t.channels, chanID)
t.mux.Unlock()
} }
// AddFunc adds a function to the list of Tracker functions such that each // AddFunc adds a function to the list of Tracker functions such that each
// function can be run after network changes. // function can be run after network changes. Returns a unique ID for the
func (t *Tracker) AddFunc(f func(isHealthy bool)) { // function.
func (t *Tracker) AddFunc(f func(isHealthy bool)) uint64 {
var currentID uint64
t.mux.Lock() t.mux.Lock()
t.funcs = append(t.funcs, f) t.funcs[t.funcsID] = f
currentID = t.funcsID
t.funcsID++
t.mux.Unlock() t.mux.Unlock()
go f(t.IsHealthy()) go f(t.IsHealthy())
return currentID
}
// RemoveFunc removes the function with the given ID from the list of Tracker
// functions so that it will not longer be run.
func (t *Tracker) RemoveFunc(chanID uint64) {
t.mux.Lock()
delete(t.channels, chanID)
t.mux.Unlock()
} }
func (t *Tracker) IsHealthy() bool { func (t *Tracker) IsHealthy() bool {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment