mirror of
https://github.com/coredns/coredns.git
synced 2025-10-27 08:14:18 -04:00
Ensure cache.ResponseWriter can be used asynchronously during prefetch (#1884)
The default dns.Response implementation of a dns.ResponseWriter will panic if RemoteAddr() is called after the connection to the client has been closed already. The current cache implementation doesn't create a new request+responsewriter during an asynchronous prefetch, but piggybacks on the request triggering the prefetch. This change copies the RemoteAddr first, so that it's safe to use it later during the actual prefetch request. A better implementation would be to completely decouple the prefetch request from the client triggering a request.
This commit is contained in:
committed by
Miek Gieben
parent
f78f30231d
commit
9c2dc7a156
36
plugin/cache/cache.go
vendored
36
plugin/cache/cache.go
vendored
@@ -4,6 +4,7 @@ package cache
|
||||
import (
|
||||
"encoding/binary"
|
||||
"hash/fnv"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin"
|
||||
@@ -105,7 +106,40 @@ type ResponseWriter struct {
|
||||
state request.Request
|
||||
server string // Server handling the request.
|
||||
|
||||
prefetch bool // When true write nothing back to the client.
|
||||
prefetch bool // When true write nothing back to the client.
|
||||
remoteAddr net.Addr
|
||||
}
|
||||
|
||||
// newPrefetchResponseWriter returns a Cache ResponseWriter to be used in
|
||||
// prefetch requests. It ensures RemoteAddr() can be called even after the
|
||||
// original connetion has already been closed.
|
||||
func newPrefetchResponseWriter(server string, state request.Request, c *Cache) *ResponseWriter {
|
||||
// Resolve the address now, the connection might be already closed when the
|
||||
// actual prefetch request is made.
|
||||
addr := state.W.RemoteAddr()
|
||||
// The protocol of the client triggering a cache prefetch doesn't matter.
|
||||
// The address type is used by request.Proto to determine the response size,
|
||||
// and using TCP ensures the message isn't unnecessarily truncated.
|
||||
if u, ok := addr.(*net.UDPAddr); ok {
|
||||
addr = &net.TCPAddr{IP: u.IP, Port: u.Port, Zone: u.Zone}
|
||||
}
|
||||
|
||||
return &ResponseWriter{
|
||||
ResponseWriter: state.W,
|
||||
Cache: c,
|
||||
state: state,
|
||||
server: server,
|
||||
prefetch: true,
|
||||
remoteAddr: addr,
|
||||
}
|
||||
}
|
||||
|
||||
// RemoteAddr implements the dns.ResponseWriter interface.
|
||||
func (w *ResponseWriter) RemoteAddr() net.Addr {
|
||||
if w.remoteAddr != nil {
|
||||
return w.remoteAddr
|
||||
}
|
||||
return w.ResponseWriter.RemoteAddr()
|
||||
}
|
||||
|
||||
// WriteMsg implements the dns.ResponseWriter interface.
|
||||
|
||||
Reference in New Issue
Block a user