Skip to content

Commit

Permalink
proxy: add query duration
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Oct 26, 2023
1 parent 39f15ce commit b6c4409
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions proxy/dnscontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ type DNSContext struct {
// StartTime is the moment when request processing started.
StartTime time.Time

// QueryDuration is the time duration of the successfull query.
QueryDuration time.Duration

// Conn is the underlying client connection. It is nil if Proto is
// ProtoDNSCrypt, ProtoHTTPS, or ProtoQUIC.
Conn net.Conn
Expand Down
7 changes: 7 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,9 @@ func (p *Proxy) replyFromUpstream(d *DNSContext) (ok bool, err error) {
if err != nil && p.Fallbacks != nil {
log.Debug("proxy: replying from upstream: using fallback due to %s", err)

// Reset the timer.
start = time.Now()

upstreams = p.Fallbacks.getUpstreamsForDomain(req.Question[0].Name)
if len(upstreams) == 0 {
return false, fmt.Errorf("selecting fallback upstream: %w", upstream.ErrNoUpstreams)
Expand All @@ -531,6 +534,10 @@ func (p *Proxy) replyFromUpstream(d *DNSContext) (ok bool, err error) {
resp, u, err = upstream.ExchangeParallel(upstreams, req)
}

if resp != nil {
d.QueryDuration = time.Since(start)
}

p.handleExchangeResult(d, req, resp, u)

return resp != nil, err
Expand Down

0 comments on commit b6c4409

Please sign in to comment.