-
Notifications
You must be signed in to change notification settings - Fork 0
/
persist.go
207 lines (180 loc) · 5.17 KB
/
persist.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
package chained
import (
"encoding/csv"
"fmt"
"os"
"path/filepath"
"strconv"
"sync"
"time"
"github.com/getlantern/flashlight/v7/balancer"
)
var (
statsTrackingDialers []balancer.Dialer
statsMx sync.Mutex
persistOnce sync.Once
)
// TrackStatsFor enables periodic checkpointing of the given proxies' stats to
// disk.
func TrackStatsFor(dialers []balancer.Dialer, configDir string, probeIfNecessary bool) {
statsMx.Lock()
statsFilePath := filepath.Join(configDir, "proxystats.csv")
applyExistingStats(statsFilePath, dialers)
if probeIfNecessary && len(dialers) > 1 {
probeIfRequired(dialers)
}
statsTrackingDialers = make([]balancer.Dialer, 0, len(dialers))
for _, d := range dialers {
statsTrackingDialers = append(statsTrackingDialers, d)
}
statsMx.Unlock()
persistOnce.Do(func() {
go persistStats(statsFilePath)
})
}
func probeIfRequired(dialers []balancer.Dialer) {
sorted := balancer.SortDialers(dialers)
rttOfTopProxy := sorted[0].EstRTT()
for i, dialer := range sorted {
// probe is automatically required for relatively new dialers
probeRequired := dialer.Attempts() < 20
if probeRequired {
log.Debugf("%v is relatively new, will probe for performance", dialer.Label())
} else if i > 0 && dialer.Successes() > 0 && dialer.EstRTT() < rttOfTopProxy {
// dialers whose RTT is lower than the top proxy get checked on startup as well
log.Debugf("%v is lower RTT than %v, will probe for performance", dialer.Label(), sorted[0].Label())
probeRequired = true
}
if probeRequired {
go dialer.Probe(true)
}
}
}
func applyExistingStats(statsFile string, dialers []balancer.Dialer) {
dialersMap := make(map[string]balancer.Dialer, len(dialers))
for _, d := range dialers {
dialersMap[d.Addr()] = d
}
in, err := os.Open(statsFile)
if err != nil {
if !os.IsNotExist(err) {
log.Errorf("Error opening stats file, will remove: %v", err)
os.Remove(statsFile)
}
return
}
defer in.Close()
csvIn := csv.NewReader(in)
rows, err := csvIn.ReadAll()
if err != nil {
log.Errorf("Unable to read proxystats.csv, will remove: %v", err)
os.Remove(statsFile)
return
}
successful := true
for _, row := range rows {
d := dialersMap[row[0]]
if d != nil {
updateErr := updateStats(d.(*proxy), row)
if updateErr != nil {
log.Errorf("Error updating stats, will remove proxystats.csv: %v", updateErr)
successful = false
break
}
log.Debugf("Loaded stats for %v", row[1])
}
}
in.Close()
if !successful {
os.Remove(statsFile)
}
}
func updateStats(p *proxy, row []string) error {
if len(row) < 10 {
return fmt.Errorf("Wrong number of fields in row")
}
attempts, err := strconv.ParseInt(row[2], 10, 64)
if err != nil {
return err
}
successes, err := strconv.ParseInt(row[3], 10, 64)
if err != nil {
return err
}
consecSuccesses, err := strconv.ParseInt(row[4], 10, 64)
if err != nil {
return err
}
failures, err := strconv.ParseInt(row[5], 10, 64)
if err != nil {
return err
}
consecFailures, err := strconv.ParseInt(row[6], 10, 64)
if err != nil {
return err
}
emaRTT, err := time.ParseDuration(row[7])
if err != nil {
return err
}
mostRecentABETime, err := time.Parse(time.RFC3339Nano, row[8])
if err != nil {
return err
}
abe, err := strconv.ParseInt(row[9], 10, 64)
if err != nil {
return err
}
var emaSuccessRate float64
if len(row) > 10 {
emaSuccessRate, err = strconv.ParseFloat(row[10], 64)
if err != nil {
return err
}
}
p.setStats(attempts, successes, consecSuccesses, failures, consecFailures, emaRTT, mostRecentABETime, abe, emaSuccessRate)
return nil
}
func persistStats(statsFilePath string) {
for {
time.Sleep(15 * time.Second)
statsMx.Lock()
dialers := make([]balancer.Dialer, 0, len(statsTrackingDialers))
for _, d := range statsTrackingDialers {
dialers = append(dialers, d)
}
statsMx.Unlock()
doPersistStats(statsFilePath, dialers)
}
}
func doPersistStats(statsFile string, dialers []balancer.Dialer) {
out, err := os.OpenFile(fmt.Sprintf("%v.tmp", statsFile), os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
log.Errorf("Unable to create temp file to save proxystats.csv: %v", err)
return
}
defer out.Close()
csvOut := csv.NewWriter(out)
csvOut.Write([]string{"addr", "label", "attempts", "successes", "consec successes", "failures", "consec failures", "est rtt", "most recent bandwidth estimate", "est bandwidth", "est success rate"})
for _, d := range dialers {
p := d.(*proxy)
p.mx.Lock()
err = csvOut.Write([]string{d.Addr(), d.Label(), fmt.Sprint(d.Attempts()), fmt.Sprint(d.Successes()), fmt.Sprint(d.ConsecSuccesses()), fmt.Sprint(d.Failures()), fmt.Sprint(d.ConsecFailures()), p.emaRTT.GetDuration().String(), p.mostRecentABETime.Format(time.RFC3339Nano), fmt.Sprint(p.abe), fmt.Sprint(p.emaSuccessRate.Get())})
p.mx.Unlock()
if err != nil {
log.Errorf("Error writing to proxystats.csv: %v", err)
return
}
}
csvOut.Flush()
err = out.Close()
if err != nil {
log.Errorf("Unable to close temporary proxystats.csv: %v", err)
return
}
err = os.Rename(out.Name(), statsFile)
if err != nil {
log.Errorf("Unable to move temporary proxystats.csv to final location: %v", err)
}
log.Debugf("Saved proxy stats to %v", statsFile)
}