Skip to content

Commit

Permalink
refactor main loops; improve wait output
Browse files Browse the repository at this point in the history
  • Loading branch information
dolph committed Feb 2, 2023
1 parent edc7c3c commit 58dfaf4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
43 changes: 26 additions & 17 deletions connectivity.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,34 @@ func main() {
}
}

// At this point we know that all destinations are valid, so we can start
// checking them. Do we want to exit once all checks pass, or run as a
// monitor daemon?
if exitOnSuccess == "1" {
var wg sync.WaitGroup
for _, dest := range destinations {
wg.Add(1)
go func(dest *Destination) {
defer wg.Done()
dest.WaitFor()
}(dest)
}

wg.Wait()
WaitForConnectivity(destinations)
} else {
// At this point we know that all destinations are valid, so we can start
// monitoring each of them.
for _, dest := range destinations {
go dest.Monitor()
}
MonitorConnectivityForever(destinations)
}
}

// Sleep forever
select {}
func WaitForConnectivity(destinations []*Destination) {
var wg sync.WaitGroup
for _, dest := range destinations {
wg.Add(1)
go func(dest *Destination) {
defer wg.Done()
dest.WaitFor()
}(dest)
}

wg.Wait()
}

func MonitorConnectivityForever(destinations []*Destination) {
for _, dest := range destinations {
go dest.Monitor()
}

// Sleep forever
select {}
}
1 change: 1 addition & 0 deletions destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ func (dest *Destination) WaitFor() {
reachable := dest.Check()

if reachable {
log.Printf("Connected to %s (%s)", dest, dest.Protocol)
return
}

Expand Down

0 comments on commit 58dfaf4

Please sign in to comment.