Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
client
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
bb751690
Commit
bb751690
authored
3 years ago
by
Jono Wenger
Committed by
Benjamin Wenger
3 years ago
Browse files
Options
Downloads
Patches
Plain Diff
XX-3354 / add unregister to health tracker
parent
99528327
No related branches found
No related tags found
1 merge request
!23
Release
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
bindings/client.go
+9
-4
9 additions, 4 deletions
bindings/client.go
interfaces/healthTracker.go
+4
-2
4 additions, 2 deletions
interfaces/healthTracker.go
network/health/tracker.go
+42
-9
42 additions, 9 deletions
network/health/tracker.go
with
55 additions
and
15 deletions
bindings/client.go
+
9
−
4
View file @
bb751690
...
...
@@ -256,10 +256,15 @@ func (c *Client) IsNetworkHealthy() bool {
return
c
.
api
.
GetHealth
()
.
IsHealthy
()
}
// registers the network health callback to be called any time the network
// health changes
func
(
c
*
Client
)
RegisterNetworkHealthCB
(
nhc
NetworkHealthCallback
)
{
c
.
api
.
GetHealth
()
.
AddFunc
(
nhc
.
Callback
)
// RegisterNetworkHealthCB registers the network health callback to be called
// any time the network health changes. Returns a unique ID that can be used to
// unregister the network health 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
...
...
This diff is collapsed.
Click to expand it.
interfaces/healthTracker.go
+
4
−
2
View file @
bb751690
...
...
@@ -8,8 +8,10 @@
package
interfaces
type
HealthTracker
interface
{
AddChannel
(
chan
bool
)
AddFunc
(
f
func
(
bool
))
AddChannel
(
chan
bool
)
uint64
RemoveChannel
(
uint64
)
AddFunc
(
f
func
(
bool
))
uint64
RemoveFunc
(
uint64
)
IsHealthy
()
bool
WasHealthy
()
bool
}
This diff is collapsed.
Click to expand it.
network/health/tracker.go
+
42
−
9
View file @
bb751690
...
...
@@ -24,8 +24,10 @@ type Tracker struct {
heartbeat
chan
network
.
Heartbeat
channels
[]
chan
bool
funcs
[]
func
(
isHealthy
bool
)
channels
map
[
uint64
]
chan
bool
funcs
map
[
uint64
]
func
(
isHealthy
bool
)
channelsID
uint64
funcsID
uint64
running
bool
...
...
@@ -51,7 +53,8 @@ func Init(instance *network.Instance, timeout time.Duration) *Tracker {
func
newTracker
(
timeout
time
.
Duration
)
*
Tracker
{
return
&
Tracker
{
timeout
:
timeout
,
channels
:
[]
chan
bool
{},
channels
:
map
[
uint64
]
chan
bool
{},
funcs
:
map
[
uint64
]
func
(
isHealthy
bool
){},
heartbeat
:
make
(
chan
network
.
Heartbeat
,
100
),
isHealthy
:
false
,
running
:
false
,
...
...
@@ -59,26 +62,56 @@ func newTracker(timeout time.Duration) *Tracker {
}
// AddChannel adds a channel to the list of Tracker channels such that each
// channel can be notified of network changes.
func
(
t
*
Tracker
)
AddChannel
(
c
chan
bool
)
{
// channel can be notified of network changes. Returns a unique ID for the
// channel.
func
(
t
*
Tracker
)
AddChannel
(
c
chan
bool
)
uint64
{
var
currentID
uint64
t
.
mux
.
Lock
()
t
.
channels
=
append
(
t
.
channels
,
c
)
t
.
channels
[
t
.
channelsID
]
=
c
currentID
=
t
.
channelsID
t
.
channelsID
++
t
.
mux
.
Unlock
()
select
{
case
c
<-
t
.
IsHealthy
()
:
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
// function can be run after network changes.
func
(
t
*
Tracker
)
AddFunc
(
f
func
(
isHealthy
bool
))
{
// function can be run after network changes. Returns a unique ID for the
// function.
func
(
t
*
Tracker
)
AddFunc
(
f
func
(
isHealthy
bool
))
uint64
{
var
currentID
uint64
t
.
mux
.
Lock
()
t
.
funcs
=
append
(
t
.
funcs
,
f
)
t
.
funcs
[
t
.
funcsID
]
=
f
currentID
=
t
.
funcsID
t
.
funcsID
++
t
.
mux
.
Unlock
()
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment