-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mux.go
95 lines (80 loc) · 1.68 KB
/
mux.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
package main
import (
"context"
"fmt"
"time"
"github.com/miekg/dns"
)
// HandleFunc is a type alias for the handler function
// from the dns package.
type HandleFunc func(dns.ResponseWriter, *dns.Msg)
// Convert returns a handler for the DNS server as well as a
// read-only channel of requests to be pushed down the pipeline.
func Convert(
pCtx context.Context,
logger Logger,
metrics bool,
) (HandleFunc, <-chan *Request) {
out := make(chan *Request)
go func() {
// Cleanup the channel when the system exists
<-pCtx.Done()
close(out)
}()
return func(w dns.ResponseWriter, req *dns.Msg) {
ctx, cancel := context.WithCancel(pCtx)
var writer Writer = w
if metrics {
writer = &metricWriter{
ctx: ctx,
logger: logger,
req: req,
start: time.Now(),
next: w.WriteMsg,
}
}
r := &Request{
ctx: ctx,
cancel: cancel,
w: writer,
r: req,
server: w.LocalAddr().String(),
client: w.RemoteAddr().String(),
}
select {
case <-pCtx.Done():
w.Close()
case out <- r:
// TODO: Log request?
}
}, out
}
type metricWriter struct {
ctx context.Context
logger Logger
req *dns.Msg
start time.Time
next func(*dns.Msg) error
}
func (m *metricWriter) WriteMsg(res *dns.Msg) error {
defer func() {
m.logger.Debugw(
"wrote response",
"duration", time.Since(m.start),
"name", res.Question[0].Name,
"type", dns.Type(res.Question[0].Qtype),
"answers", res.Answer,
)
}()
return m.next(res)
}
type Metric struct {
Domain string
Duration time.Duration
}
func (m *Metric) String() string {
return fmt.Sprintf("%s %s", m.Domain, m.Duration)
}
func (m *Metric) Event() string {
return m.String()
}