-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
207 lines (175 loc) · 5.14 KB
/
main.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
//go:build linux
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"golang.org/x/sys/unix"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// Get the RTT from the OS itself rather than timing it ourselves
// NB: Only works on linux
func tcpOsRtt(conn *net.TCPConn) (int, error) {
raw, err := conn.SyscallConn()
if err != nil {
return 0, err
}
var info *unix.TCPInfo
ctrlErr := raw.Control(func(fd uintptr) {
info, err = unix.GetsockoptTCPInfo(int(fd), unix.IPPROTO_TCP, unix.TCP_INFO)
})
switch {
case ctrlErr != nil:
return 0, ctrlErr
case err != nil:
return 0, err
}
return int(info.Rtt), nil
}
func recordLatencies(ticker *time.Ticker) {
for range ticker.C {
for _, r := range regionLatencies {
// connect over TCP to all the servers
conn, err := net.Dial("tcp", r.host)
if err != nil {
log.Printf("Unable to connect to %s: %v", r.region, err)
continue
}
// tell the server your source region
fmt.Fprintf(conn, currRegion+"\n")
// read the server's region
scanner := bufio.NewScanner(conn)
scanner.Scan()
serverRegion := scanner.Text()
// get the RTT
latency, err := tcpOsRtt(conn.(*net.TCPConn))
conn.Close()
if err != nil {
log.Printf("Unable to extract rtt from tcp conn on client to %s: %v", r.region, err)
continue
}
// update the prometheus metrics
r.hist.Observe(float64(latency))
r.last = latency
log.Printf("C:\t%s\t%s\t%d", currRegion, serverRegion, latency)
}
}
}
type regionData struct {
hist prometheus.Histogram
last int // the last latency reading
region string // the shortened region name to which this a client connected
host string // hostname for connecting to region
}
func NewRegion(r string) *regionData {
return ®ionData{
hist: promauto.NewHistogram(
prometheus.HistogramOpts{
Name: fmt.Sprintf("latency_%s_to_%s_microsecond", currRegion, r),
}),
region: r,
host: fmt.Sprintf("%s.%s.internal:%s", r, appName, tcpPort),
}
}
var regionLatencies = make(map[string]*regionData)
// TXT records contain all the deployed regions
// At some interval, refresh the information and create new regions if they don't exist
func updateRegions(ticker *time.Ticker) {
for range ticker.C {
entries, err := net.LookupTXT(fmt.Sprintf("regions.%s.internal", appName))
if err != nil {
log.Printf("TXT lookup for all deployed regions failed: %v", err)
}
if len(entries) == 0 {
log.Printf("No TXT records, skipping update")
continue
}
if len(entries) > 1 {
log.Printf("Multiple TXT records, using first")
}
entries = strings.Split(entries[0], ",")
// TODO: Drop old regions from the map?
for _, r := range entries {
if _, ok := regionLatencies[r]; !ok {
regionLatencies[r] = NewRegion(r)
}
}
}
}
// simple HTTP method to get all the latencies to all other regions in the given region
func getLatencies(w http.ResponseWriter, r *http.Request) {
for _, r := range regionLatencies {
io.WriteString(w, fmt.Sprintf("%s\t%s\t%d\n", currRegion, r.region, r.last))
}
}
// listen for clients (peers) on TCP so they can measure latency to you
func runTcpPingServer() {
listener, err := net.Listen("tcp", ":"+tcpPort)
if err != nil {
log.Fatal(err)
}
for {
conn, err := listener.Accept()
if err != nil {
log.Printf("Failed to accept a client connection: %v", err)
}
go func(c *net.TCPConn) {
defer c.Close()
// send your region to the client
fmt.Fprintf(c, currRegion+"\n")
// read the client's region
scanner := bufio.NewScanner(c)
scanner.Scan()
clientRegion := scanner.Text()
// record what the server's perceived latency is
latency, err := tcpOsRtt(c)
if err != nil {
log.Printf("Unable to extract rtt from tcp conn on server: %v", err)
return
}
log.Printf("S:\t%s\t%s\t%d", currRegion, clientRegion, latency)
//hold the conn open for the client so everything can close cleanly
time.Sleep(250 * time.Millisecond)
}(conn.(*net.TCPConn))
}
}
var appNameEnvVar = "FLY_APP_NAME"
var appName = ""
var currRegionEnvVar = "FLY_REGION"
var currRegion = ""
var regionRefreshRate = 10 * time.Second
var latencyRefreshRate = 1 * time.Second
var tcpPort = "10000"
var httpPort = "9091"
func main() {
var ok bool
currRegion, ok = os.LookupEnv(currRegionEnvVar)
if !ok || len(currRegion) == 0 {
log.Fatal(fmt.Sprintf("%s is unset", currRegionEnvVar))
}
appName, ok = os.LookupEnv(appNameEnvVar)
if !ok || len(currRegion) == 0 {
log.Fatal(fmt.Sprintf("%s is unset", appNameEnvVar))
}
regionRefreshTicker := time.NewTicker(regionRefreshRate)
defer regionRefreshTicker.Stop()
go updateRegions(regionRefreshTicker)
updateLatencyTicker := time.NewTicker(latencyRefreshRate)
defer updateLatencyTicker.Stop()
go recordLatencies(updateLatencyTicker)
go runTcpPingServer()
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", getLatencies)
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(currRegion))
})
log.Fatal(http.ListenAndServe(":"+httpPort, nil))
}