-
Notifications
You must be signed in to change notification settings - Fork 31
/
aws.go
152 lines (129 loc) · 3.18 KB
/
aws.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
package awsping
import (
"fmt"
"sync"
"time"
)
// CheckType describes a type for a check
type CheckType int
const (
// CheckTypeTCP is TCP type of check
CheckTypeTCP CheckType = iota
// CheckTypeHTTP is HTTP type of check
CheckTypeHTTP
// CheckTypeHTTPS is HTTPS type of check
CheckTypeHTTPS
)
// --------------------------------------------
// AWSRegion description of the AWS EC2 region
type AWSRegion struct {
Name string
Code string
Service string
Latencies []time.Duration
Error error
CheckType CheckType
Target Targetter
Request Requester
}
// NewRegion creates a new region with a name and code
func NewRegion(name, code string) AWSRegion {
return AWSRegion{
Name: name,
Code: code,
CheckType: CheckTypeTCP,
Request: NewAWSRequest(),
}
}
// CheckLatency does a latency check for a region
func (r *AWSRegion) CheckLatency(wg *sync.WaitGroup) {
defer wg.Done()
if r.CheckType == CheckTypeHTTP || r.CheckType == CheckTypeHTTPS {
r.checkLatencyHTTP(r.CheckType == CheckTypeHTTPS)
} else {
r.checkLatencyTCP()
}
}
// checkLatencyHTTP Test Latency via HTTP
func (r *AWSRegion) checkLatencyHTTP(https bool) {
url := r.Target.GetURL()
l, err := r.Request.Do(useragent, url, RequestTypeHTTP)
if err != nil {
r.Error = err
return
}
r.Latencies = append(r.Latencies, l)
}
// checkLatencyTCP Test Latency via TCP
func (r *AWSRegion) checkLatencyTCP() {
tcpAddr, err := r.Target.GetIP()
if err != nil {
r.Error = err
return
}
l, err := r.Request.Do(useragent, tcpAddr.String(), RequestTypeTCP)
if err != nil {
r.Error = err
return
}
r.Latencies = append(r.Latencies, l)
}
// GetLatency returns Latency in ms
func (r *AWSRegion) GetLatency() float64 {
sum := float64(0)
for _, l := range r.Latencies {
sum += Duration2ms(l)
}
return sum / float64(len(r.Latencies))
}
// GetLatencyStr returns Latency in string
func (r *AWSRegion) GetLatencyStr() string {
if r.Error != nil {
return r.Error.Error()
}
return fmt.Sprintf("%.2f ms", r.GetLatency())
}
// --------------------------------------------
// AWSRegions slice of the AWSRegion
type AWSRegions []AWSRegion
// Len returns a count of regions
func (rs AWSRegions) Len() int {
return len(rs)
}
// Less return a result of latency compare between two regions
func (rs AWSRegions) Less(i, j int) bool {
return rs[i].GetLatency() < rs[j].GetLatency()
}
// Swap two regions by index
func (rs AWSRegions) Swap(i, j int) {
rs[i], rs[j] = rs[j], rs[i]
}
// SetService sets service for all regions
func (rs AWSRegions) SetService(service string) {
for i := range rs {
rs[i].Service = service
}
}
// SetCheckType sets Check Type for all regions
func (rs AWSRegions) SetCheckType(checkType CheckType) {
for i := range rs {
rs[i].CheckType = checkType
}
}
// SetDefaultTarget sets default target instance
func (rs AWSRegions) SetDefaultTarget() {
rs.SetTarget(func(r *AWSRegion) {
r.Target = &AWSTarget{
HTTPS: r.CheckType == CheckTypeHTTPS,
Code: r.Code,
Service: r.Service,
Rnd: mkRandomString(13),
}
})
}
// SetTarget sets default target instance for all regions
func (rs AWSRegions) SetTarget(fn func(r *AWSRegion)) {
for i := range rs {
fn(&rs[i])
}
}