Improvements when offline
Signed-off-by: Marcus Noble <github@marcusnoble.co.uk>
This commit is contained in:
17
main.go
17
main.go
@@ -31,8 +31,13 @@ func init() {
|
|||||||
func main() {
|
func main() {
|
||||||
go (func() {
|
go (func() {
|
||||||
for {
|
for {
|
||||||
checkOnline()
|
result, wait := checkOnline()
|
||||||
time.Sleep(time.Minute * time.Duration(interval))
|
if result {
|
||||||
|
isOnline = 1
|
||||||
|
} else {
|
||||||
|
isOnline = 0
|
||||||
|
}
|
||||||
|
time.Sleep(time.Minute * time.Duration(wait))
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
@@ -42,15 +47,17 @@ func main() {
|
|||||||
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkOnline() {
|
func checkOnline() (bool, int) {
|
||||||
client := http.Client{
|
client := http.Client{
|
||||||
Timeout: time.Duration(timeout) * time.Millisecond,
|
Timeout: time.Duration(timeout) * time.Millisecond,
|
||||||
}
|
}
|
||||||
_, err := client.Get(endpoint)
|
_, err := client.Get(endpoint)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
isOnline = 0
|
fmt.Printf("Failed to access endpoint: %v\n", err)
|
||||||
|
// Re-check again after a minute when internet down
|
||||||
|
return false, 1
|
||||||
} else {
|
} else {
|
||||||
isOnline = 1
|
return true, interval
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user