mirror of
https://github.com/MedzikUser/go-pingbot
synced 2024-08-14 23:57:07 +00:00
54 lines
833 B
Go
54 lines
833 B
Go
package backend
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/MedzikUser/go-utils/common"
|
|
"gitlab.com/gaming0skar123/go/pingbot/config"
|
|
)
|
|
|
|
var cacheRetry int
|
|
|
|
func ping() int {
|
|
if cacheRetry >= config.Toml.Backend.Cache {
|
|
cache(0)
|
|
cacheRetry = 0
|
|
}
|
|
cacheRetry++
|
|
|
|
for _, url := range cacheURL {
|
|
go loop(url)
|
|
}
|
|
|
|
return len(cacheURL)
|
|
}
|
|
|
|
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") {
|
|
Status.Error++
|
|
|
|
return
|
|
}
|
|
|
|
client := http.DefaultClient
|
|
r, err := client.Do(req)
|
|
if err != nil {
|
|
Status.Error++
|
|
|
|
return
|
|
}
|
|
|
|
if r.StatusCode >= 200 && r.StatusCode < 400 {
|
|
Status.Success++
|
|
} else {
|
|
Status.Error++
|
|
}
|
|
}
|