Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

capture configuration OnTimer #1194

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 61 additions & 3 deletions input_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package goreplay

import (
"bytes"
"github.com/buger/goreplay/internal/capture"
"github.com/buger/goreplay/internal/tcp"
"github.com/buger/goreplay/proto"
"io/ioutil"
"net"
"net/http"
Expand All @@ -16,6 +13,10 @@ import (
"sync/atomic"
"testing"
"time"

"github.com/buger/goreplay/internal/capture"
"github.com/buger/goreplay/internal/tcp"
"github.com/buger/goreplay/proto"
)

const testRawExpire = time.Millisecond * 200
Expand Down Expand Up @@ -355,3 +356,60 @@ func BenchmarkRAWInputWithReplay(b *testing.B) {
b.ReportMetric(float64(replayCounter), "replayed")
emitter.Close()
}

func TestRAWInputOnTimer(t *testing.T) {
listener, err := net.Listen("tcp4", "127.0.0.1:0")
if err != nil {
t.Error(err)
return
}
defer listener.Close()

origin := &http.Server{
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ab"))
}),
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
}
go origin.Serve(listener)

wg := new(sync.WaitGroup)
wg.Add(1)
called := false
onTimer := func() {
if !called {
called = true
wg.Done()
}
}

conf := RAWInputConfig{
OnTimer: onTimer,
}
input := NewRAWInput(listener.Addr().String(), conf)
output := NewTestOutput(func(_ *Message) {})
plugins := &InOutPlugins{
Inputs: []PluginReader{input},
Outputs: []PluginWriter{output},
}
plugins.All = append(plugins.All, input, output)

emitter := NewEmitter()
defer emitter.Close()
go emitter.Start(plugins, Settings.Middleware)

_, port, _ := net.SplitHostPort(listener.Addr().String())
addr := "http://127.0.0.1:" + port
_, err = http.Get(addr)
if err != nil {
t.Error(err)
return
}

wg.Wait()

if !called {
t.Error("want call OnTimer")
}
}
12 changes: 9 additions & 3 deletions internal/capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"errors"
"expvar"
"fmt"
"github.com/buger/goreplay/internal/size"
"github.com/buger/goreplay/internal/tcp"
"github.com/buger/goreplay/proto"
"io"
"log"
"net"
Expand All @@ -18,6 +15,10 @@ import (
"syscall"
"time"

"github.com/buger/goreplay/internal/size"
"github.com/buger/goreplay/internal/tcp"
"github.com/buger/goreplay/proto"

"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
Expand Down Expand Up @@ -68,6 +69,8 @@ type PcapOptions struct {
AllowIncomplete bool `json:"input-raw-allow-incomplete"`
IgnoreInterface []string `json:"input-raw-ignore-interface"`
Transport string

OnTimer func() `json:"-"`
}

// Listener handle traffic capture, this is its representation.
Expand Down Expand Up @@ -552,6 +555,9 @@ func (l *Listener) readHandle(key string, hndl packetHandle) {
case <-l.quit:
return
case <-timer.C:
if l.config.OnTimer != nil {
l.config.OnTimer()
}
if h, ok := hndl.handler.(PcapStatProvider); ok {
s, err := h.Stats()
if err == nil {
Expand Down