-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_list.go
169 lines (138 loc) · 3.52 KB
/
service_list.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package main
import (
"fmt"
"net"
"strings"
"sync"
"gopkg.in/yaml.v2"
)
type urlInfo struct {
status string
isAvailable bool
retries int
}
type service struct {
Name string
Sites []string
isAvailable bool
}
type serviceList struct {
Services []service
statuses map[string]*urlInfo
output chan string
quiet bool
sync.RWMutex
}
func ServiceList(sources [][]byte, quiet bool) (*serviceList, error) {
var services, temp serviceList
services.statuses = make(map[string]*urlInfo)
services.output = make(chan string)
services.quiet = quiet
for _, source := range sources {
err := yaml.Unmarshal(source, &temp)
if err != nil {
return nil, err
}
services.Services = append(services.Services, temp.Services...)
}
return &services, nil
}
func (s *serviceList) write(done chan int) {
for {
x := <-s.output
fmt.Print(x)
if x == fmt.Sprintf("\t %s\n", green("Available")) || x == fmt.Sprintf("\t %s\n", red("Unavailable")) {
done <- 0
}
}
}
func (s *serviceList) safeLookup(key string) (*urlInfo, bool) {
s.RLock()
value, found := s.statuses[key]
s.RUnlock()
return value, found
}
func (s *serviceList) safeWrite(key string, value *urlInfo) {
s.Lock()
s.statuses[key] = value
s.Unlock()
}
func (s *serviceList) testUrl(url string, available chan bool) {
url, _ = formatUrl(url)
info, found := s.safeLookup(url)
if !found {
info = &urlInfo{
status: "",
isAvailable: false,
retries: 0,
}
for proceed := true; proceed; proceed = (!info.isAvailable && info.retries < client.maxRetries) {
if strings.HasPrefix(url, "http") {
request, err := client.newRequest(url)
if err != nil {
info.status = err.Error()
} else {
response, err := client.Do(request)
if err != nil {
if strings.Contains(err.Error(), "Timeout exceeded") {
info.status = "Request timeout exceeded"
} else if strings.Contains(err.Error(), "no such host") {
info.status = "No such host"
} else {
info.status = err.Error()
}
} else {
info.status = response.Status
}
}
info.isAvailable = getAvailability(info.status)
} else {
connection, err := net.DialTimeout("tcp", url, client.Timeout)
if err != nil {
info.status = err.Error()
} else {
info.isAvailable = true
info.status = "TCP Dial OK"
}
if connection != nil {
connection.Close()
}
}
info.retries++
}
s.safeWrite(url, info)
}
formattedStatus := formatStatus(url, info.status)
if !s.quiet || !info.isAvailable {
s.output <- fmt.Sprintf("\t %s %s %s\n", "URL:", cyan(url), formattedStatus)
}
available <- info.isAvailable
}
func (s *serviceList) testService(serviceIndex int) {
available := make(chan bool)
s.output <- fmt.Sprintf("%s %s\n", "Service:", s.Services[serviceIndex].Name)
for _, url := range s.Services[serviceIndex].Sites {
go s.testUrl(url, available)
}
isAvailable := true
for i := 0; i < len(s.Services[serviceIndex].Sites); i++ {
isAvailable = <-available && isAvailable
}
if isAvailable {
s.output <- fmt.Sprintf("\t %s\n", green("Available"))
} else {
s.output <- fmt.Sprintf("\t %s\n", red("Unavailable"))
}
s.Services[serviceIndex].isAvailable = isAvailable
}
func (s *serviceList) displayResults() {
fmt.Printf("\n%s\n", green("Available Services"))
for _, service := range s.Services {
if service.isAvailable {
fmt.Printf("%s\n", service.Name)
} else {
defer fmt.Printf("%s\n", service.Name)
}
}
fmt.Printf("\n%s\n", red("Unavailable Services"))
}