Skip to content

Commit

Permalink
upstream: imp code, logging
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Jan 23, 2024
1 parent cea34d5 commit 0632b4f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions upstream/quic.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ type dnsOverQUIC struct {
bytesPool *sync.Pool

// quicConfigMu protects quicConfig.
quicConfigMu sync.Mutex
quicConfigMu *sync.Mutex

// connMu protects conn.
connMu sync.Mutex
connMu *sync.Mutex

// bytesPoolGuard protects bytesPool.
bytesPoolMu sync.Mutex
bytesPoolMu *sync.Mutex

// timeout is the timeout for the upstream connection.
timeout time.Duration
Expand Down Expand Up @@ -118,7 +118,10 @@ func newDoQ(addr *url.URL, opts *Options) (u Upstream, err error) {
VerifyConnection: opts.VerifyConnection,
NextProtos: compatProtoDQ,
},
timeout: opts.Timeout,
quicConfigMu: &sync.Mutex{},
connMu: &sync.Mutex{},
bytesPoolMu: &sync.Mutex{},
timeout: opts.Timeout,
}

runtime.SetFinalizer(u, (*dnsOverQUIC).Close)
Expand Down Expand Up @@ -159,7 +162,7 @@ func (p *dnsOverQUIC) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
// connection was closed (due to inactivity for example) AND the server
// refuses to open a 0-RTT connection.
for i := 0; hasConnection && p.shouldRetry(err) && i < 2; i++ {
log.Debug("re-creating the QUIC connection and retrying due to %v", err)
log.Debug("dnsproxy: re-creating the QUIC connection and retrying due to %v", err)

// Close the active connection to make sure we'll try to re-connect.
p.closeConnWithError(err)
Expand Down Expand Up @@ -228,7 +231,7 @@ func (p *dnsOverQUIC) exchangeQUIC(req *dns.Msg) (resp *dns.Msg, err error) {
// write-direction of the stream, but does not prevent reading from it.
err = stream.Close()
if err != nil {
log.Debug("closing quic stream: %s", err)
log.Debug("dnsproxy: closing quic stream: %s", err)
}

return p.readMsg(stream)
Expand Down Expand Up @@ -277,7 +280,7 @@ func (p *dnsOverQUIC) getConnection(useCached bool) (c quic.Connection, err erro
// We're recreating the connection, let's create a new one.
err = conn.CloseWithError(QUICCodeNoError, "")
if err != nil {
log.Debug("closing stale connection: %s", err)
log.Debug("dnsproxy: closing stale connection: %s", err)
}
}

Expand Down Expand Up @@ -325,7 +328,7 @@ func (p *dnsOverQUIC) openStream(conn quic.Connection) (quic.Stream, error) {

stream, err := conn.OpenStreamSync(ctx)
if err != nil {
log.Debug("opening quic stream: %s", err)
log.Debug("dnsproxy: opening quic stream: %s", err)
} else {
return stream, nil
}
Expand Down Expand Up @@ -359,7 +362,7 @@ func (p *dnsOverQUIC) openConnection() (conn quic.Connection, err error) {
// It's never actually used.
err = rawConn.Close()
if err != nil {
log.Debug("closing raw connection for %s: %s", p.addr, err)
log.Debug("dnsproxy: closing raw connection for %s: %s", p.addr, err)
}

udpConn, ok := rawConn.(*net.UDPConn)
Expand Down Expand Up @@ -404,7 +407,7 @@ func (p *dnsOverQUIC) closeConnWithError(err error) {

err = p.conn.CloseWithError(code, "")
if err != nil {
log.Error("failed to close the conn: %v", err)
log.Error("dnsproxy: failed to close the conn: %v", err)
}
p.conn = nil
}
Expand Down

0 comments on commit 0632b4f

Please sign in to comment.