Skip to content

Commit

Permalink
Add support for "go" timestamp source
Browse files Browse the repository at this point in the history
Windows having issues with generating timestamps, so adding application level timestamp generation
Made small refactoring to move "accurate-enough" time to own package.
  • Loading branch information
buger committed Jul 7, 2021
1 parent 59b12ab commit 8edb74e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
8 changes: 7 additions & 1 deletion capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ func (l *Listener) PcapHandle(ifi pcap.Interface) (handle *pcap.Handle, err erro
}
defer inactive.CleanUp()

if l.TimestampType != "" {
if l.TimestampType != "" && l.TimestampType != "go" {
var ts pcap.TimestampSource
ts, err = pcap.TimestampSourceFromString(l.TimestampType)
fmt.Println("Setting custom Timestamp Source. Supported values: `simple`, ", inactive.SupportedTimestamps())
err = inactive.SetTimestampSource(ts)
if err != nil {
return nil, fmt.Errorf("%q: supported timestamps: %q, interface: %q", err, inactive.SupportedTimestamps(), ifi.Name)
Expand Down Expand Up @@ -345,7 +346,12 @@ func (l *Listener) read(handler PacketHandler) {
default:
data, ci, err := hndl.handler.ZeroCopyReadPacketData()
if err == nil {
if l.TimestampType == "go" {
ci.Timestamp = time.Now()
}

pckt, err := tcp.ParsePacket(data, linkType, linkSize, &ci, false)

if err == nil {
for _, p := range l.ports {
if pckt.DstPort == p {
Expand Down
17 changes: 17 additions & 0 deletions simpletime/time.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package simpletime

import (
"time"
)

var Now time.Time

func init() {
go func() {
for {
// Accurate enough
Now = time.Now()
time.Sleep(100 * time.Millisecond)
}
}()
}
5 changes: 3 additions & 2 deletions tcp/tcp_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"
"unsafe"

"github.com/buger/goreplay/simpletime"
"github.com/buger/goreplay/size"
)

Expand Down Expand Up @@ -39,7 +40,7 @@ func NewBufferPool(max int, ttl int) *bufPool {
for i := 0; i < 100; i++ {
select {
case c := <-pool.buffers:
if now.Sub(c.created) < time.Duration(ttl)*time.Second {
if simpletime.Now.Sub(c.created) < time.Duration(ttl)*time.Second {
select {
case pool.buffers <- c:
default:
Expand Down Expand Up @@ -81,7 +82,7 @@ func (p *bufPool) Get() *buf {

c = new(buf)
c.b = make([]byte, 1024)
c.created = now
c.created = simpletime.Now

// Use this technique to find if pool leaks, and objects get GCd
//
Expand Down
16 changes: 4 additions & 12 deletions tcp/tcp_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"runtime/debug"
"time"

"github.com/buger/goreplay/simpletime"

"github.com/google/gopacket"
)

Expand All @@ -31,8 +33,6 @@ func copySlice(to []byte, from ...[]byte) ([]byte, int) {
return to, i
}

var now time.Time

var stats *expvar.Map
var bufPoolCount *expvar.Int
var releasedCount *expvar.Int
Expand All @@ -45,14 +45,6 @@ func init() {
stats.Init()
stats.Set("buffer_pool_count", bufPoolCount)
stats.Set("buffer_released", releasedCount)

go func() {
for {
// Accurate enough
now = time.Now()
time.Sleep(500 * time.Millisecond)
}
}()
}

var packetPool = NewPacketPool(10000, 1)
Expand All @@ -79,7 +71,7 @@ func NewPacketPool(max int, ttl int) *pktPool {
select {
case c := <-pool.packets:
// GC If buffer is too big and lived for too long
if len(c.buf) < 8192 || now.Sub(c.created) < time.Duration(ttl)*time.Second {
if len(c.buf) < 8192 || simpletime.Now.Sub(c.created) < time.Duration(ttl)*time.Second {
select {
case pool.packets <- c:
// Jump to next item in for loop
Expand Down Expand Up @@ -120,7 +112,7 @@ func (p *pktPool) Get() *Packet {
default:
stats.Add("active_packet_count", 1)
c = new(Packet)
c.created = now
c.created = simpletime.Now

// Use this technique to find if pool leaks, and objects get GCd
//
Expand Down

0 comments on commit 8edb74e

Please sign in to comment.