Repository navigation
Expand file tree
/
Copy pathtcppool.go
More file actions
285 lines (246 loc) · 7.55 KB
/
Copy pathtcppool.go
File metadata and controls
285 lines (246 loc) · 7.55 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package resolver
import (
"bufio"
"context"
"encoding/binary"
"fmt"
"io"
"net"
"sync"
"sync/atomic"
"time"
"github.com/miekg/dns"
)
// tcpPoolConns is the number of persistent TCP connections per nameserver.
// Each connection supports pipelining — multiple queries in-flight
// simultaneously, dispatched by DNS transaction ID. With 2000 workers
// and 13 TLD nameservers, each connection sees ~2000/(13*8) ≈ 19
// concurrent queries — well within TCP pipelining capacity.
const tcpPoolConns = 8
// maxPendingPerConn limits concurrent in-flight queries per connection.
// This prevents overloading authoritative servers and ensures queries
// get timely responses. Excess queries wait briefly for a slot.
const maxPendingPerConn = 100
// tcpIdleTimeout is how long an idle TCP connection is kept open.
const tcpIdleTimeout = 60 * time.Second
// tcpDialTimeout is the timeout for establishing a new TCP connection.
const tcpDialTimeout = 2 * time.Second
// tcpPool maintains persistent pipelined TCP connections to a single
// DNS nameserver. Multiple goroutines can issue queries concurrently
// on the same connection — the write mutex is held only for the
// duration of the write (~microseconds), and responses are dispatched
// asynchronously by a dedicated reader goroutine.
type tcpPool struct {
addr string
conns [tcpPoolConns]*pipelinedConn // 8 persistent connections
mu sync.Mutex
next uint32
}
// globalTCPPools reuses TCP connection pools across nameserver objects
// that share the same address. When a zone expires and is re-created,
// the new nameserver objects reuse existing TCP connections instead of
// dialing fresh ones.
var (
globalPoolsMu sync.Mutex
globalPools = make(map[string]*tcpPool)
)
func newTCPPool(addr string) *tcpPool {
return &tcpPool{addr: addr}
}
func getTCPPool(addr string) *tcpPool {
globalPoolsMu.Lock()
defer globalPoolsMu.Unlock()
if p, ok := globalPools[addr]; ok {
return p
}
p := &tcpPool{addr: addr}
globalPools[addr] = p
return p
}
// preDial establishes TCP connections in the background so they're
// ready when the first query arrives. Called during zone creation.
func (p *tcpPool) preDial() {
go func() {
for i := 0; i < tcpPoolConns; i++ {
_, _ = p.getConn(i) // best-effort pre-dial; failures will be retried on first real Exchange
}
}()
}
// Exchange sends a DNS query over a pipelined TCP connection and waits
// for the response. The caller's context controls the timeout.
func (p *tcpPool) Exchange(ctx context.Context, m *dns.Msg) (*dns.Msg, time.Duration, error) {
start := time.Now()
// Round-robin connection selection.
idx := atomic.AddUint32(&p.next, 1) % tcpPoolConns
pc, err := p.getConn(int(idx))
if err != nil {
return nil, time.Since(start), err
}
resp, err := pc.exchange(ctx, m)
return resp, time.Since(start), err
}
// getConn returns an existing pipelined connection or creates a new one.
func (p *tcpPool) getConn(idx int) (*pipelinedConn, error) {
p.mu.Lock()
pc := p.conns[idx]
if pc != nil && pc.alive() {
p.mu.Unlock()
return pc, nil
}
// Need new connection.
p.mu.Unlock()
conn, err := net.DialTimeout("tcp", p.addr, tcpDialTimeout)
if err != nil {
return nil, fmt.Errorf("tcp dial %s: %w", p.addr, err)
}
pc = newPipelinedConn(conn)
p.mu.Lock()
// Another goroutine may have created a connection while we dialed.
if existing := p.conns[idx]; existing != nil && existing.alive() {
p.mu.Unlock()
_ = conn.Close() // discarding losing-race connection: close error is irrelevant
return existing, nil
}
p.conns[idx] = pc
p.mu.Unlock()
return pc, nil
}
// pipelinedConn wraps a single TCP connection with support for
// multiple concurrent in-flight DNS queries. Queries are identified
// by their DNS transaction ID. A dedicated reader goroutine dispatches
// responses to waiting callers.
type pipelinedConn struct {
conn net.Conn
writer *bufio.Writer
wmu sync.Mutex // protects writes only
sem chan struct{} // limits concurrent in-flight queries
pending sync.Map // map[uint16]chan *dns.Msg
nextID atomic.Uint32
closed atomic.Bool
done chan struct{}
}
func newPipelinedConn(conn net.Conn) *pipelinedConn {
pc := &pipelinedConn{
conn: conn,
writer: bufio.NewWriterSize(conn, 4096),
sem: make(chan struct{}, maxPendingPerConn),
done: make(chan struct{}),
}
go pc.readLoop()
return pc
}
func (pc *pipelinedConn) alive() bool {
return !pc.closed.Load()
}
// exchange sends a query and waits for the response matched by DNS ID.
func (pc *pipelinedConn) exchange(ctx context.Context, m *dns.Msg) (*dns.Msg, error) {
if pc.closed.Load() {
return nil, fmt.Errorf("connection closed")
}
// Limit concurrent in-flight queries per connection to avoid
// overwhelming the remote server and ensure timely responses.
select {
case pc.sem <- struct{}{}:
defer func() { <-pc.sem }()
case <-ctx.Done():
return nil, ctx.Err()
}
// Assign a unique DNS transaction ID.
id := uint16(pc.nextID.Add(1))
origID := m.Id
m.Id = id
// Register a channel to receive the response.
ch := make(chan *dns.Msg, 1)
pc.pending.Store(id, ch)
defer func() {
pc.pending.Delete(id)
m.Id = origID // restore original ID
}()
// Write the query (length-prefixed per TCP DNS framing).
data, err := m.Pack()
if err != nil {
return nil, err
}
pc.wmu.Lock()
// Set write deadline from context.
deadline, ok := ctx.Deadline()
if ok {
_ = pc.conn.SetWriteDeadline(deadline) // network setup: error surfaces on the subsequent Write
} else {
_ = pc.conn.SetWriteDeadline(time.Now().Add(5 * time.Second)) // network setup: error surfaces on the subsequent Write
}
var lenBuf [2]byte
binary.BigEndian.PutUint16(lenBuf[:], uint16(len(data)))
_, err = pc.writer.Write(lenBuf[:])
if err == nil {
_, err = pc.writer.Write(data)
}
if err == nil {
err = pc.writer.Flush()
}
pc.wmu.Unlock()
if err != nil {
pc.close()
return nil, fmt.Errorf("tcp write: %w", err)
}
// Wait for response or context cancellation.
select {
case resp := <-ch:
if resp == nil {
return nil, fmt.Errorf("connection closed while waiting")
}
resp.Id = origID // restore caller's original ID
return resp, nil
case <-ctx.Done():
return nil, ctx.Err()
case <-pc.done:
return nil, fmt.Errorf("connection closed while waiting")
}
}
// readLoop reads DNS responses from the TCP connection and dispatches
// them to waiting callers by DNS transaction ID.
func (pc *pipelinedConn) readLoop() {
defer pc.close()
var lenBuf [2]byte
for {
// Read 2-byte length prefix.
_ = pc.conn.SetReadDeadline(time.Now().Add(tcpIdleTimeout)) // network setup: error surfaces on the subsequent Read
if _, err := io.ReadFull(pc.conn, lenBuf[:]); err != nil {
return
}
msgLen := binary.BigEndian.Uint16(lenBuf[:])
if msgLen == 0 {
return
}
// Read the DNS message.
buf := make([]byte, msgLen)
if _, err := io.ReadFull(pc.conn, buf); err != nil {
return
}
// Parse and dispatch.
msg := new(dns.Msg)
if err := msg.Unpack(buf); err != nil {
continue // skip malformed message
}
if ch, ok := pc.pending.Load(msg.Id); ok {
ch.(chan *dns.Msg) <- msg
}
// If no pending entry, the caller timed out — discard.
}
}
// close shuts down the connection and wakes all pending callers.
func (pc *pipelinedConn) close() {
if pc.closed.CompareAndSwap(false, true) {
_ = pc.conn.Close() // shutting down: close error is irrelevant
close(pc.done)
// Wake all pending callers with nil (they'll get an error).
pc.pending.Range(func(key, value any) bool {
ch := value.(chan *dns.Msg)
select {
case ch <- nil:
default:
}
return true
})
}
}