Files
coredns/plugin/dnstap/dnstapio/io.go

147 lines
3.0 KiB
Go
Raw Normal View History

package dnstapio
import (
2017-11-28 00:36:14 +03:00
"net"
"sync/atomic"
2017-11-28 00:36:14 +03:00
"time"
clog "github.com/coredns/coredns/plugin/pkg/log"
tap "github.com/dnstap/golang-dnstap"
2017-11-28 00:36:14 +03:00
fs "github.com/farsightsec/golang-framestream"
)
var log = clog.NewWithPlugin("dnstap")
2017-11-28 00:36:14 +03:00
const (
tcpWriteBufSize = 1024 * 1024
tcpTimeout = 4 * time.Second
flushTimeout = 1 * time.Second
queueSize = 10000
2017-11-28 00:36:14 +03:00
)
type dnstapIO struct {
endpoint string
socket bool
conn net.Conn
enc *dnstapEncoder
queue chan tap.Dnstap
dropped uint32
quit chan struct{}
}
2017-11-28 00:36:14 +03:00
// New returns a new and initialized DnstapIO.
func New(endpoint string, socket bool) DnstapIO {
return &dnstapIO{
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{}),
}
2017-11-28 00:36:14 +03:00
}
2017-11-28 00:36:14 +03:00
// DnstapIO interface
type DnstapIO interface {
Connect()
2017-11-28 00:36:14 +03:00
Dnstap(payload tap.Dnstap)
Close()
}
func (dio *dnstapIO) newConnect() error {
2017-11-28 00:36:14 +03:00
var err error
if dio.socket {
if dio.conn, err = net.Dial("unix", dio.endpoint); err != nil {
return err
}
2017-11-28 00:36:14 +03:00
} else {
if dio.conn, err = net.DialTimeout("tcp", dio.endpoint, tcpTimeout); err != nil {
return err
}
if tcpConn, ok := dio.conn.(*net.TCPConn); ok {
tcpConn.SetWriteBuffer(tcpWriteBufSize)
tcpConn.SetNoDelay(false)
}
2017-11-28 00:36:14 +03:00
}
return dio.enc.resetWriter(dio.conn)
}
// Connect connects to the dnstop endpoint.
func (dio *dnstapIO) Connect() {
if err := dio.newConnect(); err != nil {
log.Error("No connection to dnstap endpoint")
}
go dio.serve()
}
// Dnstap enqueues the payload for log.
2017-11-28 00:36:14 +03:00
func (dio *dnstapIO) Dnstap(payload tap.Dnstap) {
select {
case dio.queue <- payload:
default:
atomic.AddUint32(&dio.dropped, 1)
}
}
func (dio *dnstapIO) closeConnection() {
dio.enc.close()
if dio.conn != nil {
dio.conn.Close()
dio.conn = nil
}
}
2017-11-28 00:36:14 +03:00
// Close waits until the I/O routine is finished to return.
func (dio *dnstapIO) Close() {
close(dio.quit)
2017-11-28 00:36:14 +03:00
}
func (dio *dnstapIO) flushBuffer() {
if dio.conn == nil {
if err := dio.newConnect(); err != nil {
return
}
log.Info("Reconnected to dnstap")
}
if err := dio.enc.flushBuffer(); err != nil {
log.Warningf("Connection lost: %s", err)
dio.closeConnection()
if err := dio.newConnect(); err != nil {
log.Errorf("Cannot connect to dnstap: %s", err)
} else {
log.Info("Reconnected to dnstap")
}
}
}
func (dio *dnstapIO) write(payload *tap.Dnstap) {
if err := dio.enc.writeMsg(payload); err != nil {
atomic.AddUint32(&dio.dropped, 1)
}
}
2017-11-28 00:36:14 +03:00
func (dio *dnstapIO) serve() {
timeout := time.After(flushTimeout)
for {
select {
case <-dio.quit:
dio.flushBuffer()
dio.closeConnection()
return
case payload := <-dio.queue:
dio.write(&payload)
2017-11-28 00:36:14 +03:00
case <-timeout:
if dropped := atomic.SwapUint32(&dio.dropped, 0); dropped > 0 {
log.Warningf("Dropped dnstap messages: %d", dropped)
}
dio.flushBuffer()
2017-11-28 00:36:14 +03:00
timeout = time.After(flushTimeout)
}
}
}