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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
elixxir
client
Commits
99528327
Commit
99528327
authored
Jun 21, 2021
by
Jono Wenger
Browse files
Options
Downloads
Patches
Plain Diff
Reformat comments and code
parent
9584c95b
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!23
Release
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
network/health/tracker.go
+24
-14
24 additions, 14 deletions
network/health/tracker.go
network/health/tracker_test.go
+7
-14
7 additions, 14 deletions
network/health/tracker_test.go
with
31 additions
and
28 deletions
network/health/tracker.go
+
24
−
14
View file @
99528327
...
...
@@ -5,7 +5,8 @@
// LICENSE file //
///////////////////////////////////////////////////////////////////////////////
// Contains functionality related to the event model driven network health tracker
// Contains functionality related to the event model driven network health
// tracker.
package
health
...
...
@@ -30,63 +31,68 @@ type Tracker struct {
// Determines the current health status
isHealthy
bool
// Denotes the past health status
// wasHealthy is true if isHealthy has ever been true
// Denotes that the past health status wasHealthy is true if isHealthy has
// ever been true
wasHealthy
bool
mux
sync
.
RWMutex
}
// Creates a single HealthTracker thread, starts it, and returns a tracker and a stoppable
// Init creates a single HealthTracker thread, starts it, and returns a tracker
// and a stoppable.
func
Init
(
instance
*
network
.
Instance
,
timeout
time
.
Duration
)
*
Tracker
{
tracker
:=
newTracker
(
timeout
)
instance
.
SetNetworkHealthChan
(
tracker
.
heartbeat
)
return
tracker
}
//
B
uilds and returns a new Tracker object given a Context
//
newTracker b
uilds and returns a new Tracker object given a Context
.
func
newTracker
(
timeout
time
.
Duration
)
*
Tracker
{
return
&
Tracker
{
timeout
:
timeout
,
channels
:
make
(
[]
chan
bool
,
0
)
,
channels
:
[]
chan
bool
{}
,
heartbeat
:
make
(
chan
network
.
Heartbeat
,
100
),
isHealthy
:
false
,
running
:
false
,
}
}
// Add a channel to the list of Tracker channels
//
such that each
channel can be notified of network changes
// Add
Channel 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
)
{
t
.
mux
.
Lock
()
t
.
channels
=
append
(
t
.
channels
,
c
)
t
.
mux
.
Unlock
()
select
{
case
c
<-
t
.
IsHealthy
()
:
default
:
}
}
// Add a function to the list of Tracker function
//
such that each
function can be run after network changes
// Add
Func adds
a function to the list of Tracker function
s such that each
// function can be run after network changes
.
func
(
t
*
Tracker
)
AddFunc
(
f
func
(
isHealthy
bool
))
{
t
.
mux
.
Lock
()
t
.
funcs
=
append
(
t
.
funcs
,
f
)
t
.
mux
.
Unlock
()
go
f
(
t
.
IsHealthy
())
}
func
(
t
*
Tracker
)
IsHealthy
()
bool
{
t
.
mux
.
RLock
()
defer
t
.
mux
.
RUnlock
()
return
t
.
isHealthy
}
//
R
eturns true if isHealthy has ever been true
//
WasHealthy r
eturns true if isHealthy has ever been true
.
func
(
t
*
Tracker
)
WasHealthy
()
bool
{
t
.
mux
.
RLock
()
defer
t
.
mux
.
RUnlock
()
return
t
.
wasHealthy
}
...
...
@@ -94,10 +100,11 @@ func (t *Tracker) setHealth(h bool) {
t
.
mux
.
Lock
()
// Only set wasHealthy to true if either
// wasHealthy is true or
// wasHealthy false but h value is true
// wasHealthy
is
false but h value is true
t
.
wasHealthy
=
t
.
wasHealthy
||
h
t
.
isHealthy
=
h
t
.
mux
.
Unlock
()
t
.
transmit
(
h
)
}
...
...
@@ -119,7 +126,8 @@ func (t *Tracker) Start() (stoppable.Stoppable, error) {
return
stop
,
nil
}
// Long-running thread used to monitor and report on network health
// start starts a long-running thread used to monitor and report on network
// health.
func
(
t
*
Tracker
)
start
(
stop
*
stoppable
.
Single
)
{
timer
:=
time
.
NewTimer
(
t
.
timeout
)
...
...
@@ -131,8 +139,10 @@ func (t *Tracker) start(stop *stoppable.Single) {
t
.
isHealthy
=
false
t
.
running
=
false
t
.
mux
.
Unlock
()
t
.
transmit
(
false
)
stop
.
ToStopped
()
return
case
heartbeat
=
<-
t
.
heartbeat
:
if
healthy
(
heartbeat
)
{
...
...
This diff is collapsed.
Click to expand it.
network/health/tracker_test.go
+
7
−
14
View file @
99528327
...
...
@@ -9,12 +9,11 @@ package health
import
(
"gitlab.com/elixxir/comms/network"
// "gitlab.com/elixxir/comms/network"
"testing"
"time"
)
// Happy path smoke test
// Happy path smoke test
.
func
TestNewTracker
(
t
*
testing
.
T
)
{
// Initialize required variables
timeout
:=
250
*
time
.
Millisecond
...
...
@@ -49,8 +48,7 @@ func TestNewTracker(t *testing.T) {
// Begin the health tracker
_
,
err
:=
tracker
.
Start
()
if
err
!=
nil
{
t
.
Errorf
(
"Unable to start tracker: %+v"
,
err
)
return
t
.
Fatalf
(
"Unable to start tracker: %+v"
,
err
)
}
// Send a positive health heartbeat
...
...
@@ -68,14 +66,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as healthy
if
!
tracker
.
IsHealthy
()
{
t
.
Errorf
(
"Tracker did not become healthy"
)
return
t
.
Fatal
(
"Tracker did not become healthy."
)
}
// Check if the tracker was ever healthy
if
!
tracker
.
WasHealthy
()
{
t
.
Errorf
(
"Tracker did not become healthy"
)
return
t
.
Fatal
(
"Tracker did not become healthy."
)
}
// Verify the heartbeat triggered the listening chan/func
...
...
@@ -89,15 +85,12 @@ func TestNewTracker(t *testing.T) {
// Verify the network was marked as NOT healthy
if
tracker
.
IsHealthy
()
{
t
.
Errorf
(
"Tracker should not report healthy"
)
return
t
.
Fatal
(
"Tracker should not report healthy."
)
}
// Check if the tracker was ever healthy,
// after setting healthy to false
// Check if the tracker was ever healthy, after setting healthy to false
if
!
tracker
.
WasHealthy
()
{
t
.
Errorf
(
"Tracker was healthy previously but not reported healthy"
)
return
t
.
Fatal
(
"Tracker was healthy previously but not reported healthy."
)
}
// Verify the timeout triggered the listening chan/func
...
...
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