-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.go
56 lines (38 loc) · 918 Bytes
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package updater
import (
"log"
"net/http"
"github.com/Abbas-gheydi/hanoproxy/model"
)
func (rec *recordTable) checkHttpHealth(r model.Record) (updatedRecord model.Record) {
for i := range r.Ip {
go func(iteration int) {
healthstatus := isURLhealthy(r.Ip[iteration].Url, r.Options.Expected_Response_Code, r.Options.RetryCount)
rec.update(&r.Ip[iteration], healthstatus)
}(i)
}
return r
}
func isURLhealthy(url string, code int, retryCount int) bool {
health := false
defer func() {
if err := recover(); err != nil {
log.Println("http panic occurred:", err)
}
}()
for i := 0; i <= retryCount; i++ {
resp, err := http.Get(url)
if err != nil {
log.Print("http", err)
health = false
} else {
defer resp.Body.Close()
if resp.StatusCode == code {
health = true
log.Println(url, "Response status:", resp.StatusCode)
break
}
}
}
return health
}