-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
rate.go
236 lines (203 loc) · 5.1 KB
/
rate.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright © by Jeff Foley 2022-2024. All rights reserved.
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
// SPDX-License-Identifier: Apache-2.0
package resolve
import (
"strings"
"sync"
"time"
"github.com/miekg/dns"
"go.uber.org/ratelimit"
"golang.org/x/net/publicsuffix"
)
const (
startQPSPerNameserver = 15
maxQPSPerNameserver = 250
numIntervalSeconds = 2
rateUpdateInterval = numIntervalSeconds * time.Second
maxTimeoutPercentage = 0.5
)
type rateTrack struct {
sync.Mutex
qps int
rate ratelimit.Limiter
success int
timeout int
}
type RateTracker struct {
sync.Mutex
done chan struct{}
domainToServers map[string][]string
serverToLimiter map[string]*rateTrack
catchLimiter *rateTrack
}
// NewRateTracker returns an active RateTracker that tracks and rate limits per name server.
func NewRateTracker() *RateTracker {
r := &RateTracker{
done: make(chan struct{}, 1),
domainToServers: make(map[string][]string),
serverToLimiter: make(map[string]*rateTrack),
catchLimiter: newRateTrack(),
}
go r.updateRateLimiters()
return r
}
func newRateTrack() *rateTrack {
return &rateTrack{
qps: startQPSPerNameserver,
rate: ratelimit.New(startQPSPerNameserver),
}
}
// Stop will release the RateTracker resources.
func (r *RateTracker) Stop() {
select {
case <-r.done:
default:
close(r.done)
}
}
// Take blocks as required by the implemented rate limiter for the associated name server.
func (r *RateTracker) Take(sub string) {
tracker := r.getDomainRateTracker(sub)
tracker.Lock()
rate := tracker.rate
tracker.Unlock()
rate.Take()
}
// Success signals to the RateTracker that a request for the provided subdomain name was successful.
func (r *RateTracker) Success(sub string) {
tracker := r.getDomainRateTracker(sub)
tracker.Lock()
tracker.success++
tracker.Unlock()
}
// Timeout signals to the RateTracker that a request for the provided subdomain name timed out.
func (r *RateTracker) Timeout(sub string) {
tracker := r.getDomainRateTracker(sub)
tracker.Lock()
tracker.timeout++
tracker.Unlock()
}
func (r *RateTracker) updateRateLimiters() {
t := time.NewTimer(rateUpdateInterval)
defer t.Stop()
for {
select {
case <-r.done:
return
case <-t.C:
r.updateAllRateLimiters()
t.Reset(rateUpdateInterval)
}
}
}
func (r *RateTracker) updateAllRateLimiters() {
r.Lock()
defer r.Unlock()
r.catchLimiter.update()
for _, rt := range r.serverToLimiter {
rt.update()
}
}
func (rt *rateTrack) update() {
rt.Lock()
defer rt.Unlock()
// check if this rate tracker has already been updated
if rt.success == 0 && rt.timeout == 0 {
return
}
// timeouts in excess of maxTimeoutPercentage indicate a need to slow down
if float64(rt.timeout)/float64(rt.success+rt.timeout) > maxTimeoutPercentage {
rt.qps -= 1
if rt.qps <= 0 {
rt.qps = 1
}
} else {
rt.qps += 1
if rt.qps > maxQPSPerNameserver {
rt.qps = maxQPSPerNameserver
}
}
// update the QPS rate limiter and reset counters
rt.rate = ratelimit.New(rt.qps)
rt.success = 0
rt.timeout = 0
}
func (r *RateTracker) getDomainRateTracker(sub string) *rateTrack {
r.Lock()
defer r.Unlock()
n := strings.ToLower(RemoveLastDot(sub))
domain, err := publicsuffix.EffectiveTLDPlusOne(n)
if err != nil {
return r.catchLimiter
}
domain = strings.ToLower(RemoveLastDot(domain))
servers := r.getMappedServers(n, domain)
if len(servers) == 0 {
return r.catchLimiter
}
var tracker *rateTrack
// check if we already have a rate limiter for these servers
for _, name := range servers {
if rt, found := r.serverToLimiter[name]; found {
tracker = rt
break
}
}
if tracker == nil {
tracker = newRateTrack()
}
// make sure all the servers are using the same rate limiter
for _, name := range servers {
if _, found := r.serverToLimiter[name]; !found {
r.serverToLimiter[name] = tracker
}
}
return tracker
}
func (r *RateTracker) getMappedServers(sub, domain string) []string {
var servers []string
FQDNToRegistered(sub, domain, func(name string) bool {
if serv, found := r.domainToServers[name]; found {
servers = serv
return true
}
return false
})
if len(servers) == 0 {
if servs, zone := r.deepestNameServers(sub, domain); zone != "" && len(servs) > 0 {
r.domainToServers[zone] = servs
servers = servs
}
}
return servers
}
func (r *RateTracker) deepestNameServers(sub, domain string) ([]string, string) {
var zone string
var servers []string
FQDNToRegistered(sub, domain, func(name string) bool {
var found bool
if s := r.getNameservers(name); len(s) > 0 {
zone = name
servers = s
found = true
}
return found
})
return servers, zone
}
func (r *RateTracker) getNameservers(domain string) []string {
client := dns.Client{
Net: "tcp",
Timeout: time.Minute,
}
var servers []string
if m, _, err := client.Exchange(QueryMsg(domain, dns.TypeNS), "8.8.8.8:53"); err == nil {
if ans := ExtractAnswers(m); len(ans) > 0 {
for _, rr := range AnswersByType(ans, dns.TypeNS) {
servers = append(servers, strings.ToLower(RemoveLastDot(rr.Data)))
}
}
}
return servers
}