Skip to content

Commit 8edb74e

Browse files
committed
Add support for "go" timestamp source
Windows having issues with generating timestamps, so adding application level timestamp generation Made small refactoring to move "accurate-enough" time to own package.
1 parent 59b12ab commit 8edb74e

File tree

4 files changed

+31
-15
lines changed

4 files changed

+31
-15
lines changed

capture/capture.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,10 @@ func (l *Listener) PcapHandle(ifi pcap.Interface) (handle *pcap.Handle, err erro
229229
}
230230
defer inactive.CleanUp()
231231

232-
if l.TimestampType != "" {
232+
if l.TimestampType != "" && l.TimestampType != "go" {
233233
var ts pcap.TimestampSource
234234
ts, err = pcap.TimestampSourceFromString(l.TimestampType)
235+
fmt.Println("Setting custom Timestamp Source. Supported values: `simple`, ", inactive.SupportedTimestamps())
235236
err = inactive.SetTimestampSource(ts)
236237
if err != nil {
237238
return nil, fmt.Errorf("%q: supported timestamps: %q, interface: %q", err, inactive.SupportedTimestamps(), ifi.Name)
@@ -345,7 +346,12 @@ func (l *Listener) read(handler PacketHandler) {
345346
default:
346347
data, ci, err := hndl.handler.ZeroCopyReadPacketData()
347348
if err == nil {
349+
if l.TimestampType == "go" {
350+
ci.Timestamp = time.Now()
351+
}
352+
348353
pckt, err := tcp.ParsePacket(data, linkType, linkSize, &ci, false)
354+
349355
if err == nil {
350356
for _, p := range l.ports {
351357
if pckt.DstPort == p {

simpletime/time.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package simpletime
2+
3+
import (
4+
"time"
5+
)
6+
7+
var Now time.Time
8+
9+
func init() {
10+
go func() {
11+
for {
12+
// Accurate enough
13+
Now = time.Now()
14+
time.Sleep(100 * time.Millisecond)
15+
}
16+
}()
17+
}

tcp/tcp_message.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010
"unsafe"
1111

12+
"github.com/buger/goreplay/simpletime"
1213
"github.com/buger/goreplay/size"
1314
)
1415

@@ -39,7 +40,7 @@ func NewBufferPool(max int, ttl int) *bufPool {
3940
for i := 0; i < 100; i++ {
4041
select {
4142
case c := <-pool.buffers:
42-
if now.Sub(c.created) < time.Duration(ttl)*time.Second {
43+
if simpletime.Now.Sub(c.created) < time.Duration(ttl)*time.Second {
4344
select {
4445
case pool.buffers <- c:
4546
default:
@@ -81,7 +82,7 @@ func (p *bufPool) Get() *buf {
8182

8283
c = new(buf)
8384
c.b = make([]byte, 1024)
84-
c.created = now
85+
c.created = simpletime.Now
8586

8687
// Use this technique to find if pool leaks, and objects get GCd
8788
//

tcp/tcp_packet.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"runtime/debug"
1010
"time"
1111

12+
"github.com/buger/goreplay/simpletime"
13+
1214
"github.com/google/gopacket"
1315
)
1416

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

34-
var now time.Time
35-
3636
var stats *expvar.Map
3737
var bufPoolCount *expvar.Int
3838
var releasedCount *expvar.Int
@@ -45,14 +45,6 @@ func init() {
4545
stats.Init()
4646
stats.Set("buffer_pool_count", bufPoolCount)
4747
stats.Set("buffer_released", releasedCount)
48-
49-
go func() {
50-
for {
51-
// Accurate enough
52-
now = time.Now()
53-
time.Sleep(500 * time.Millisecond)
54-
}
55-
}()
5648
}
5749

5850
var packetPool = NewPacketPool(10000, 1)
@@ -79,7 +71,7 @@ func NewPacketPool(max int, ttl int) *pktPool {
7971
select {
8072
case c := <-pool.packets:
8173
// GC If buffer is too big and lived for too long
82-
if len(c.buf) < 8192 || now.Sub(c.created) < time.Duration(ttl)*time.Second {
74+
if len(c.buf) < 8192 || simpletime.Now.Sub(c.created) < time.Duration(ttl)*time.Second {
8375
select {
8476
case pool.packets <- c:
8577
// Jump to next item in for loop
@@ -120,7 +112,7 @@ func (p *pktPool) Get() *Packet {
120112
default:
121113
stats.Add("active_packet_count", 1)
122114
c = new(Packet)
123-
c.created = now
115+
c.created = simpletime.Now
124116

125117
// Use this technique to find if pool leaks, and objects get GCd
126118
//

0 commit comments

Comments
 (0)