-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrequest.go
72 lines (52 loc) · 1.55 KB
/
request.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
package request
import (
"crypto/tls"
"errors"
"fmt"
"net/http"
"strconv"
"time"
"github.com/jpillora/backoff"
"github.com/juandiii/jetson-monitor/config"
"github.com/juandiii/jetson-monitor/logging"
"github.com/juandiii/jetson-monitor/notification"
)
type Request struct {
http http.Client
}
func RequestServer(c config.URL, ns []notification.CommandProvider, log *logging.StandardLogger) (string, error) {
b := &backoff.Backoff{
Jitter: true,
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: false}
client := &http.Client{}
req, err := http.NewRequest("GET", c.URL, nil)
if err != nil {
log.Error(err)
return "", errors.New("Received an invalid status code: 500 The service might be experiencing issues")
}
for tries := 0; tries < 3; tries++ {
resp, err := client.Do(req)
if err != nil {
d := b.Duration()
time.Sleep(d)
if tries == 2 {
for _, n := range ns {
n.SendMessage(¬ification.Message{
Text: fmt.Sprintf("The server %s is down", c.URL),
})
}
return "", fmt.Errorf("The server %s is down", c.URL)
}
continue
}
defer resp.Body.Close()
if c.StatusCode != nil && *c.StatusCode != resp.StatusCode {
log.Errorf("%s \n", c.URL)
return "", errors.New("Received an invalid status code: " + strconv.Itoa(resp.StatusCode) + " The service might be experiencing issues")
}
log.Debugf("[OK] %s", c.URL)
return fmt.Sprintf("[OK] %s", c.URL), nil
}
return "", errors.New("The request failed because it wasn't able to reach the service")
}