plugin/dnstap: don't spam Travis + valid dnstap payload (#1121)

* don't spam Travis + valid dnstap payload

* log instead of fmt

* Revert "log instead of fmt"

This reverts commit 88f09c3939.

* log the right way

* log the final way

* minor enhancements
This commit is contained in:
varyoo
2017-09-29 21:29:33 +02:00
committed by Miek Gieben
parent 1e71d0e2c1
commit 45b0252c1a
2 changed files with 17 additions and 11 deletions

View File

@@ -1,8 +1,7 @@
package dnstapio package dnstapio
import ( import (
"fmt" "log"
"io"
tap "github.com/dnstap/golang-dnstap" tap "github.com/dnstap/golang-dnstap"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
@@ -10,9 +9,9 @@ import (
// DnstapIO wraps the dnstap I/O routine. // DnstapIO wraps the dnstap I/O routine.
type DnstapIO struct { type DnstapIO struct {
writer io.WriteCloser protocol Protocol
queue chan tap.Dnstap queue chan tap.Dnstap
stop chan bool stop chan bool
} }
// Protocol is either `out.TCP` or `out.Socket`. // Protocol is either `out.TCP` or `out.Socket`.
@@ -26,7 +25,7 @@ type Protocol interface {
// New dnstap I/O routine from Protocol. // New dnstap I/O routine from Protocol.
func New(w Protocol) *DnstapIO { func New(w Protocol) *DnstapIO {
dio := DnstapIO{} dio := DnstapIO{}
dio.writer = w dio.protocol = w
dio.queue = make(chan tap.Dnstap, 10) dio.queue = make(chan tap.Dnstap, 10)
dio.stop = make(chan bool) dio.stop = make(chan bool)
go dio.serve() go dio.serve()
@@ -38,7 +37,7 @@ func (dio *DnstapIO) Dnstap(payload tap.Dnstap) {
select { select {
case dio.queue <- payload: case dio.queue <- payload:
default: default:
fmt.Println("[WARN] Dnstap payload dropped.") log.Println("[WARN] Dnstap payload dropped.")
} }
} }
@@ -48,9 +47,9 @@ func (dio *DnstapIO) serve() {
case payload := <-dio.queue: case payload := <-dio.queue:
frame, err := proto.Marshal(&payload) frame, err := proto.Marshal(&payload)
if err == nil { if err == nil {
dio.writer.Write(frame) dio.protocol.Write(frame)
} else { } else {
fmt.Println("[ERROR] Invalid dnstap payload dropped.") log.Printf("[ERROR] Invalid dnstap payload dropped: %s\n", err)
} }
case <-dio.stop: case <-dio.stop:
close(dio.queue) close(dio.queue)
@@ -65,5 +64,5 @@ func (dio DnstapIO) Close() error {
dio.stop <- true dio.stop <- true
<-dio.stop <-dio.stop
close(dio.stop) close(dio.stop)
return dio.writer.Close() return dio.protocol.Close()
} }

View File

@@ -2,6 +2,8 @@ package dnstapio
import ( import (
"bytes" "bytes"
"io/ioutil"
"log"
"sync" "sync"
"testing" "testing"
"time" "time"
@@ -9,6 +11,10 @@ import (
tap "github.com/dnstap/golang-dnstap" tap "github.com/dnstap/golang-dnstap"
) )
func init() {
log.SetOutput(ioutil.Discard)
}
type buf struct { type buf struct {
*bytes.Buffer *bytes.Buffer
cost time.Duration cost time.Duration
@@ -38,7 +44,8 @@ func TestRace(t *testing.T) {
return return
default: default:
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
dio.Dnstap(tap.Dnstap{}) t := tap.Dnstap_MESSAGE
dio.Dnstap(tap.Dnstap{Type: &t})
} }
} }
}() }()