mirror of
https://github.com/coredns/coredns.git
synced 2025-11-07 20:46:59 -05:00
plugin/dnstap: remove custom encoder (#4242)
* plugin/dnstap: remove encoder*.go Those files reimplemented parts of the dnstap spec, we can just use the dnstap functions for that. This leaves all the queuing that is enabled and drops messages if the dnstap reader can't keep up. In the new code flush() would never return an error (at least I couldn't make it do so), so the reconnect functionally is moved to kick off when we get write errors. Some smaller cosmetic changes as well, `d.socket` is now `proto`, which makes the dial() function smaller. Total testing time is now <1s (which was the impetus to look into this plugin *again*). See #4238 The buffered channel needs to be sized correctly, as we may need to do some queing if the dnstap reader can't keep up. Signed-off-by: Miek Gieben <miek@miek.nl> * add missing file Signed-off-by: Miek Gieben <miek@miek.nl> * update doc on queing Signed-off-by: Miek Gieben <miek@miek.nl>
This commit is contained in:
@@ -8,16 +8,15 @@ import (
|
||||
clog "github.com/coredns/coredns/plugin/pkg/log"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
fs "github.com/farsightsec/golang-framestream"
|
||||
)
|
||||
|
||||
var log = clog.NewWithPlugin("dnstap")
|
||||
|
||||
const (
|
||||
tcpWriteBufSize = 1024 * 1024
|
||||
tcpWriteBufSize = 1024 * 1024 // there is no good explanation for why this number (see #xxx)
|
||||
queueSize = 10000 // see #xxxx
|
||||
tcpTimeout = 4 * time.Second
|
||||
flushTimeout = 1 * time.Second
|
||||
queueSize = 10000
|
||||
)
|
||||
|
||||
// Tapper interface is used in testing to mock the Dnstap method.
|
||||
@@ -27,52 +26,47 @@ type Tapper interface {
|
||||
|
||||
// dio implements the Tapper interface.
|
||||
type dio struct {
|
||||
endpoint string
|
||||
socket bool
|
||||
conn net.Conn
|
||||
enc *dnstapEncoder
|
||||
queue chan tap.Dnstap
|
||||
dropped uint32
|
||||
quit chan struct{}
|
||||
endpoint string
|
||||
proto string
|
||||
conn net.Conn
|
||||
enc *Encoder
|
||||
queue chan tap.Dnstap
|
||||
dropped uint32
|
||||
quit chan struct{}
|
||||
flushTimeout time.Duration
|
||||
tcpTimeout time.Duration
|
||||
}
|
||||
|
||||
// New returns a new and initialized pointer to a dio.
|
||||
func New(endpoint string, socket bool) *dio {
|
||||
func New(proto, endpoint string) *dio {
|
||||
return &dio{
|
||||
endpoint: endpoint,
|
||||
socket: socket,
|
||||
enc: newDnstapEncoder(&fs.EncoderOptions{
|
||||
ContentType: []byte("protobuf:dnstap.Dnstap"),
|
||||
Bidirectional: true,
|
||||
}),
|
||||
queue: make(chan tap.Dnstap, queueSize),
|
||||
quit: make(chan struct{}),
|
||||
endpoint: endpoint,
|
||||
proto: proto,
|
||||
queue: make(chan tap.Dnstap, queueSize),
|
||||
quit: make(chan struct{}),
|
||||
flushTimeout: flushTimeout,
|
||||
tcpTimeout: tcpTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dio) newConnect() error {
|
||||
var err error
|
||||
if d.socket {
|
||||
if d.conn, err = net.Dial("unix", d.endpoint); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if d.conn, err = net.DialTimeout("tcp", d.endpoint, tcpTimeout); err != nil {
|
||||
return err
|
||||
}
|
||||
if tcpConn, ok := d.conn.(*net.TCPConn); ok {
|
||||
tcpConn.SetWriteBuffer(tcpWriteBufSize)
|
||||
tcpConn.SetNoDelay(false)
|
||||
}
|
||||
func (d *dio) dial() error {
|
||||
conn, err := net.DialTimeout(d.proto, d.endpoint, d.tcpTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if tcpConn, ok := conn.(*net.TCPConn); ok {
|
||||
tcpConn.SetWriteBuffer(tcpWriteBufSize)
|
||||
tcpConn.SetNoDelay(false)
|
||||
}
|
||||
|
||||
return d.enc.resetWriter(d.conn)
|
||||
d.enc, err = newEncoder(conn, d.tcpTimeout)
|
||||
return err
|
||||
}
|
||||
|
||||
// Connect connects to the dnstap endpoint.
|
||||
func (d *dio) Connect() {
|
||||
if err := d.newConnect(); err != nil {
|
||||
log.Error("No connection to dnstap endpoint")
|
||||
if err := d.dial(); err != nil {
|
||||
log.Errorf("No connection to dnstap endpoint: %s", err)
|
||||
}
|
||||
go d.serve()
|
||||
}
|
||||
@@ -86,58 +80,46 @@ func (d *dio) Dnstap(payload tap.Dnstap) {
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dio) closeConnection() {
|
||||
d.enc.close()
|
||||
if d.conn != nil {
|
||||
d.conn.Close()
|
||||
d.conn = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Close waits until the I/O routine is finished to return.
|
||||
func (d *dio) Close() { close(d.quit) }
|
||||
|
||||
func (d *dio) flushBuffer() {
|
||||
if d.conn == nil {
|
||||
if err := d.newConnect(); err != nil {
|
||||
return
|
||||
}
|
||||
log.Info("Reconnected to dnstap")
|
||||
func (d *dio) write(payload *tap.Dnstap) error {
|
||||
if d.enc == nil {
|
||||
atomic.AddUint32(&d.dropped, 1)
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := d.enc.flushBuffer(); err != nil {
|
||||
log.Warningf("Connection lost: %s", err)
|
||||
d.closeConnection()
|
||||
if err := d.newConnect(); err != nil {
|
||||
log.Errorf("Cannot connect to dnstap: %s", err)
|
||||
} else {
|
||||
log.Info("Reconnected to dnstap")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (d *dio) write(payload *tap.Dnstap) {
|
||||
if err := d.enc.writeMsg(payload); err != nil {
|
||||
atomic.AddUint32(&d.dropped, 1)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *dio) serve() {
|
||||
timeout := time.After(flushTimeout)
|
||||
timeout := time.After(d.flushTimeout)
|
||||
for {
|
||||
select {
|
||||
case <-d.quit:
|
||||
d.flushBuffer()
|
||||
d.closeConnection()
|
||||
if d.enc == nil {
|
||||
return
|
||||
}
|
||||
d.enc.flush()
|
||||
d.enc.close()
|
||||
return
|
||||
case payload := <-d.queue:
|
||||
d.write(&payload)
|
||||
if err := d.write(&payload); err != nil {
|
||||
d.dial()
|
||||
}
|
||||
case <-timeout:
|
||||
if dropped := atomic.SwapUint32(&d.dropped, 0); dropped > 0 {
|
||||
log.Warningf("Dropped dnstap messages: %d", dropped)
|
||||
}
|
||||
d.flushBuffer()
|
||||
timeout = time.After(flushTimeout)
|
||||
if d.enc == nil {
|
||||
d.dial()
|
||||
} else {
|
||||
d.enc.flush()
|
||||
}
|
||||
timeout = time.After(d.flushTimeout)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user