|
| 1 | +package proxy |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "sync" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/AdguardTeam/dnsproxy/upstream" |
| 9 | + "github.com/miekg/dns" |
| 10 | +) |
| 11 | + |
| 12 | +// upstreamWithStats is a wrapper around the [upstream.Upstream] interface that |
| 13 | +// gathers statistics. |
| 14 | +type upstreamWithStats struct { |
| 15 | + // upstream is the upstream DNS resolver. |
| 16 | + upstream upstream.Upstream |
| 17 | + |
| 18 | + // mu protects err and queryDuration. |
| 19 | + mu *sync.Mutex |
| 20 | + |
| 21 | + // err is the DNS lookup error, if any. |
| 22 | + err error |
| 23 | + |
| 24 | + // queryDuration is the duration of the successful DNS lookup. |
| 25 | + queryDuration time.Duration |
| 26 | + |
| 27 | + // isFallback indicates whether the upstream is a fallback upstream. |
| 28 | + isFallback bool |
| 29 | +} |
| 30 | + |
| 31 | +// newUpstreamWithStats returns a new initialized *upstreamWithStats. |
| 32 | +func newUpstreamWithStats( |
| 33 | + upstream upstream.Upstream, |
| 34 | + isFallback bool, |
| 35 | +) (u *upstreamWithStats) { |
| 36 | + return &upstreamWithStats{ |
| 37 | + upstream: upstream, |
| 38 | + mu: &sync.Mutex{}, |
| 39 | + isFallback: isFallback, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// stats returns the stored statistics. |
| 44 | +func (u *upstreamWithStats) stats() (dur time.Duration, err error) { |
| 45 | + u.mu.Lock() |
| 46 | + defer u.mu.Unlock() |
| 47 | + |
| 48 | + return u.queryDuration, u.err |
| 49 | +} |
| 50 | + |
| 51 | +// type check |
| 52 | +var _ upstream.Upstream = (*upstreamWithStats)(nil) |
| 53 | + |
| 54 | +// Exchange implements the [upstream.Upstream] for *upstreamWithStats. |
| 55 | +func (u *upstreamWithStats) Exchange(req *dns.Msg) (resp *dns.Msg, err error) { |
| 56 | + start := time.Now() |
| 57 | + resp, err = u.upstream.Exchange(req) |
| 58 | + |
| 59 | + u.mu.Lock() |
| 60 | + defer u.mu.Unlock() |
| 61 | + |
| 62 | + u.err = err |
| 63 | + u.queryDuration = time.Since(start) |
| 64 | + |
| 65 | + return resp, err |
| 66 | +} |
| 67 | + |
| 68 | +// Address implements the [upstream.Upstream] for *upstreamWithStats. |
| 69 | +func (u *upstreamWithStats) Address() (addr string) { |
| 70 | + return u.upstream.Address() |
| 71 | +} |
| 72 | + |
| 73 | +// Close implements the [upstream.Upstream] for *upstreamWithStats. |
| 74 | +func (u *upstreamWithStats) Close() (err error) { |
| 75 | + return u.upstream.Close() |
| 76 | +} |
| 77 | + |
| 78 | +// upstreamsWithStats takes a list of upstreams, wraps each upstream with |
| 79 | +// [upstreamWithStats] to gather statistics, and returns the wrapped upstreams. |
| 80 | +func upstreamsWithStats( |
| 81 | + upstreams []upstream.Upstream, |
| 82 | + isFallback bool, |
| 83 | +) (wrapped []upstream.Upstream) { |
| 84 | + wrapped = make([]upstream.Upstream, 0, len(upstreams)) |
| 85 | + for _, u := range upstreams { |
| 86 | + w := newUpstreamWithStats(u, isFallback) |
| 87 | + wrapped = append(wrapped, w) |
| 88 | + } |
| 89 | + |
| 90 | + return wrapped |
| 91 | +} |
| 92 | + |
| 93 | +// QueryStatistics contains the DNS query statistics for both the upstream and |
| 94 | +// fallback DNS servers. |
| 95 | +type QueryStatistics struct { |
| 96 | + main []*UpstreamStatistics |
| 97 | + fallback []*UpstreamStatistics |
| 98 | +} |
| 99 | + |
| 100 | +// cachedQueryStatistics returns the DNS query statistics for cached queries. |
| 101 | +func cachedQueryStatistics(addr string) (s *QueryStatistics) { |
| 102 | + return &QueryStatistics{ |
| 103 | + main: []*UpstreamStatistics{{ |
| 104 | + Address: addr, |
| 105 | + IsCached: true, |
| 106 | + }}, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +// Main returns the DNS query statistics for the upstream DNS servers. |
| 111 | +func (s *QueryStatistics) Main() (us []*UpstreamStatistics) { |
| 112 | + return s.main |
| 113 | +} |
| 114 | + |
| 115 | +// Fallback returns the DNS query statistics for the fallback DNS servers. |
| 116 | +func (s *QueryStatistics) Fallback() (us []*UpstreamStatistics) { |
| 117 | + return s.fallback |
| 118 | +} |
| 119 | + |
| 120 | +// collectQueryStats gathers the statistics from the wrapped upstreams, |
| 121 | +// considering the upstream mode. resolver is an upstream DNS resolver that |
| 122 | +// successfully resolved the request, and it will be unwrapped. Provided |
| 123 | +// upstreams must be of type *upstreamWithStats. |
| 124 | +// |
| 125 | +// If the DNS query was not resolved (i.e., if resolver is nil) or upstream mode |
| 126 | +// is [UpstreamModeFastestAddr], the function returns the gathered statistics |
| 127 | +// for both the upstream and fallback DNS servers. Otherwise, it returns the |
| 128 | +// query statistics specifically for resolver. |
| 129 | +func collectQueryStats( |
| 130 | + mode UpstreamMode, |
| 131 | + resolver upstream.Upstream, |
| 132 | + upstreams []upstream.Upstream, |
| 133 | + fallbacks []upstream.Upstream, |
| 134 | +) (unwrapped upstream.Upstream, stats *QueryStatistics) { |
| 135 | + var wrapped *upstreamWithStats |
| 136 | + if resolver != nil { |
| 137 | + var ok bool |
| 138 | + wrapped, ok = resolver.(*upstreamWithStats) |
| 139 | + if !ok { |
| 140 | + // Should never happen. |
| 141 | + err := fmt.Errorf("unexpected type %T", resolver) |
| 142 | + panic(err) |
| 143 | + } |
| 144 | + |
| 145 | + unwrapped = wrapped.upstream |
| 146 | + } |
| 147 | + |
| 148 | + if wrapped == nil || mode == UpstreamModeFastestAddr { |
| 149 | + return unwrapped, &QueryStatistics{ |
| 150 | + main: collectUpstreamStats(upstreams), |
| 151 | + fallback: collectUpstreamStats(fallbacks), |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return unwrapped, collectResolverQueryStats(wrapped) |
| 156 | +} |
| 157 | + |
| 158 | +// collectResolverQueryStats gathers the statistics from an upstream DNS |
| 159 | +// resolver that successfully resolved the request. resolver must be not nil. |
| 160 | +func collectResolverQueryStats(resolver *upstreamWithStats) (stats *QueryStatistics) { |
| 161 | + dur, err := resolver.stats() |
| 162 | + s := &UpstreamStatistics{ |
| 163 | + Address: resolver.upstream.Address(), |
| 164 | + Error: err, |
| 165 | + QueryDuration: dur, |
| 166 | + } |
| 167 | + |
| 168 | + if resolver.isFallback { |
| 169 | + return &QueryStatistics{ |
| 170 | + fallback: []*UpstreamStatistics{s}, |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + return &QueryStatistics{ |
| 175 | + main: []*UpstreamStatistics{s}, |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +// UpstreamStatistics contains the DNS query statistics. |
| 180 | +type UpstreamStatistics struct { |
| 181 | + // Error is the DNS lookup error, if any. |
| 182 | + Error error |
| 183 | + |
| 184 | + // Address is the address of the upstream DNS resolver. |
| 185 | + // |
| 186 | + // TODO(s.chzhen): Use [upstream.Upstream] when [cacheItem] starts to |
| 187 | + // contain one. |
| 188 | + Address string |
| 189 | + |
| 190 | + // QueryDuration is the duration of the successful DNS lookup. |
| 191 | + QueryDuration time.Duration |
| 192 | + |
| 193 | + // IsCached indicates whether the response was served from a cache. |
| 194 | + IsCached bool |
| 195 | +} |
| 196 | + |
| 197 | +// collectUpstreamStats gathers the upstream statistics from the list of wrapped |
| 198 | +// upstreams. upstreams must be of type *upstreamWithStats. |
| 199 | +func collectUpstreamStats(upstreams []upstream.Upstream) (stats []*UpstreamStatistics) { |
| 200 | + stats = make([]*UpstreamStatistics, 0, len(upstreams)) |
| 201 | + |
| 202 | + for _, u := range upstreams { |
| 203 | + w, ok := u.(*upstreamWithStats) |
| 204 | + if !ok { |
| 205 | + // Should never happen. |
| 206 | + err := fmt.Errorf("unexpected type %T", u) |
| 207 | + panic(err) |
| 208 | + } |
| 209 | + |
| 210 | + dur, err := w.stats() |
| 211 | + stats = append(stats, &UpstreamStatistics{ |
| 212 | + Error: err, |
| 213 | + Address: w.Address(), |
| 214 | + QueryDuration: dur, |
| 215 | + }) |
| 216 | + } |
| 217 | + |
| 218 | + return stats |
| 219 | +} |
0 commit comments