-
Notifications
You must be signed in to change notification settings - Fork 1
/
check.go
79 lines (63 loc) · 1.5 KB
/
check.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"hash/fnv"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
)
func hash(b []byte) uint64 {
h := fnv.New64a()
h.Write(b)
return h.Sum64()
}
func checkSite(name string) (result Result, err error) {
start := time.Now()
resp, err := http.Get(name)
if err != nil {
return result, err
}
defer resp.Body.Close()
result.Duration = time.Since(start)
result.Time = time.Now()
result.StatusCode = resp.StatusCode
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return result, err
}
result.Length = len(body)
result.Hash = hash(body)
return result, nil
}
// GetLastResult returns the lastes result for site
func (site *Site) GetLastResult() Result {
site.Mutex.Lock()
defer site.Mutex.Unlock()
return site.LastResult
}
func (site *Site) setLastResult(result Result) {
site.Mutex.Lock()
defer site.Mutex.Unlock()
site.LastResult = result
}
// Run tests site forever
func (sitest Sitest) Run(name string) {
site := sitest.Sites[name]
// slow start
slowStart := time.Duration(rand.Float64()*site.Config.Interval.Seconds()) * time.Second
log.Printf("[%v] slow start, sleep %v", name, slowStart)
time.Sleep(slowStart)
for {
log.Printf("[%v] querying...", name)
result, err := checkSite(name)
if err != nil {
log.Printf("[%v] error %v (%v)", name, err, result)
} else {
log.Printf("[%v] success, result: %+v", name, result)
}
site.setLastResult(result)
sitest.Metrics.Update(name, result, err)
time.Sleep(site.Config.Interval - result.Duration)
}
}