go-pingbot/backend/ping.go

50 lines
804 B
Go
Raw Normal View History

2021-07-11 21:19:37 +00:00
package backend
import (
"context"
2021-07-11 21:19:37 +00:00
"net/http"
"time"
2021-07-11 21:19:37 +00:00
2021-08-12 19:26:54 +00:00
"github.com/MedzikUser/go-utils/common"
"gitlab.com/gaming0skar123/go/pingbot/config"
2021-07-11 21:19:37 +00:00
)
var cacheRetry int
2021-07-11 21:19:37 +00:00
func ping() {
if cacheRetry >= config.Toml.Backend.Cache {
cache(0)
cacheRetry = 0
2021-07-11 21:19:37 +00:00
}
cacheRetry++
for _, url := range cacheURL {
go loop(url)
2021-07-11 21:19:37 +00:00
}
}
func loop(url string) {
// Timeout 1 minute
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if common.CheckErr(err, "new http request") {
2021-07-29 18:51:15 +00:00
Status.Error++
return
}
client := http.DefaultClient
r, err := client.Do(req)
if err != nil {
2021-07-29 18:51:15 +00:00
Status.Error++
2021-07-11 21:19:37 +00:00
return
}
if r.StatusCode >= 200 && r.StatusCode < 400 {
2021-07-29 18:51:15 +00:00
Status.Success++
2021-07-11 21:19:37 +00:00
} else {
2021-07-29 18:51:15 +00:00
Status.Error++
2021-07-11 21:19:37 +00:00
}
}