mirror of
https://github.com/coredns/coredns.git
synced 2025-11-02 02:03:13 -05:00
Adds the dnstap I/O routines and should fix some issues (#1083)
* adds the dnstap I/O thread and should fix a lot of mistakes * docs * -race test * oops * docs
This commit is contained in:
69
plugin/dnstap/dnstapio/io.go
Normal file
69
plugin/dnstap/dnstapio/io.go
Normal file
@@ -0,0 +1,69 @@
|
||||
package dnstapio
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
"github.com/golang/protobuf/proto"
|
||||
)
|
||||
|
||||
// DnstapIO wraps the dnstap I/O routine.
|
||||
type DnstapIO struct {
|
||||
writer io.WriteCloser
|
||||
queue chan tap.Dnstap
|
||||
stop chan bool
|
||||
}
|
||||
|
||||
// Protocol is either `out.TCP` or `out.Socket`.
|
||||
type Protocol interface {
|
||||
// Write takes a single frame at once.
|
||||
Write([]byte) (int, error)
|
||||
|
||||
Close() error
|
||||
}
|
||||
|
||||
// New dnstap I/O routine from Protocol.
|
||||
func New(w Protocol) *DnstapIO {
|
||||
dio := DnstapIO{}
|
||||
dio.writer = w
|
||||
dio.queue = make(chan tap.Dnstap, 10)
|
||||
dio.stop = make(chan bool)
|
||||
go dio.serve()
|
||||
return &dio
|
||||
}
|
||||
|
||||
// Dnstap enqueues the payload for log.
|
||||
func (dio *DnstapIO) Dnstap(payload tap.Dnstap) {
|
||||
select {
|
||||
case dio.queue <- payload:
|
||||
default:
|
||||
fmt.Println("[WARN] Dnstap payload dropped.")
|
||||
}
|
||||
}
|
||||
|
||||
func (dio *DnstapIO) serve() {
|
||||
for {
|
||||
select {
|
||||
case payload := <-dio.queue:
|
||||
frame, err := proto.Marshal(&payload)
|
||||
if err == nil {
|
||||
dio.writer.Write(frame)
|
||||
} else {
|
||||
fmt.Println("[ERROR] Invalid dnstap payload dropped.")
|
||||
}
|
||||
case <-dio.stop:
|
||||
close(dio.queue)
|
||||
dio.stop <- true
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Close waits until the I/O routine is finished to return.
|
||||
func (dio DnstapIO) Close() error {
|
||||
dio.stop <- true
|
||||
<-dio.stop
|
||||
close(dio.stop)
|
||||
return dio.writer.Close()
|
||||
}
|
||||
71
plugin/dnstap/dnstapio/io_test.go
Normal file
71
plugin/dnstap/dnstapio/io_test.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package dnstapio
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
tap "github.com/dnstap/golang-dnstap"
|
||||
)
|
||||
|
||||
type buf struct {
|
||||
*bytes.Buffer
|
||||
cost time.Duration
|
||||
}
|
||||
|
||||
func (b buf) Write(frame []byte) (int, error) {
|
||||
time.Sleep(b.cost)
|
||||
return b.Buffer.Write(frame)
|
||||
}
|
||||
|
||||
func (b buf) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestRace(t *testing.T) {
|
||||
b := buf{&bytes.Buffer{}, 100 * time.Millisecond}
|
||||
dio := New(b)
|
||||
wg := &sync.WaitGroup{}
|
||||
wg.Add(10)
|
||||
for i := 0; i < 10; i++ {
|
||||
timeout := time.After(time.Second)
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
wg.Done()
|
||||
return
|
||||
default:
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
dio.Dnstap(tap.Dnstap{})
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestClose(t *testing.T) {
|
||||
done := make(chan bool)
|
||||
var dio *DnstapIO
|
||||
go func() {
|
||||
b := buf{&bytes.Buffer{}, 0}
|
||||
dio = New(b)
|
||||
dio.Close()
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second):
|
||||
t.Fatal("Not closing.")
|
||||
}
|
||||
func() {
|
||||
defer func() {
|
||||
if err := recover(); err == nil {
|
||||
t.Fatal("Send on closed channel.")
|
||||
}
|
||||
}()
|
||||
dio.Dnstap(tap.Dnstap{})
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user