prevent CPU churn by setting long pcap timeout

pcap timeouts are a mystery. Setting the timeout to -1 used to reduce
latency, but that was before we started setting immediate mode. And it
turns out that with that timeout, pcap chews through a lot of CPU
polling its file descriptor, checking timers, etc. However, the
behaviour of setting *no* timeout is defined as, err, undefined. So
instead we set a very long timeout indeed. And loop if it expires.

Fixes #80
This commit is contained in:
Matthias Radestock committed 2014-09-23 17:54:40 +01:00
1 parent d80a5c8dff
commit 025315363d
3 files changed
+11 -5

No files matched your search

+7 -2
View File
@@ -40,7 +40,7 @@ func newPcapIO(ifName string, promisc bool, snaplen int, bufSz int) (handle *Pca
if err = inactive.SetSnapLen(snaplen); err != nil {
return
}
if err = inactive.SetTimeout(-1); err != nil {
if err = inactive.SetTimeout(MaxDuration); err != nil {
return
}
if err = inactive.SetImmediateMode(true); err != nil {
@@ -60,7 +60,12 @@ func newPcapIO(ifName string, promisc bool, snaplen int, bufSz int) (handle *Pca
}
func (pi *PcapIO) ReadPacket() (data []byte, err error) {
data, _, err = pi.handle.ZeroCopyReadPacketData()
for {
data, _, err = pi.handle.ZeroCopyReadPacketData()
if err == nil || err != pcap.NextErrorTimeoutExpired {
break
}
}
return
}